You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.3 KiB
62 lines
2.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Mvc.Server.Models;
|
|
using OpenIddict.Abstractions;
|
|
using OpenIddict.Server.AspNetCore;
|
|
using static OpenIddict.Abstractions.OpenIddictConstants;
|
|
|
|
namespace Mvc.Server.Controllers
|
|
{
|
|
public class UserinfoController : Controller
|
|
{
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
public UserinfoController(UserManager<ApplicationUser> userManager)
|
|
=> _userManager = userManager;
|
|
|
|
//
|
|
// GET: /api/userinfo
|
|
[Authorize(AuthenticationSchemes = OpenIddictServerAspNetCoreDefaults.AuthenticationScheme)]
|
|
[HttpGet("~/connect/userinfo"), Produces("application/json")]
|
|
public async Task<IActionResult> Userinfo()
|
|
{
|
|
var user = await _userManager.GetUserAsync(User);
|
|
if (user == null)
|
|
{
|
|
return Challenge(OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
|
|
}
|
|
|
|
var claims = new Dictionary<string, object>(StringComparer.Ordinal)
|
|
{
|
|
// Note: the "sub" claim is a mandatory claim and must be included in the JSON response.
|
|
[Claims.Subject] = await _userManager.GetUserIdAsync(user)
|
|
};
|
|
|
|
if (User.HasScope(Scopes.Email))
|
|
{
|
|
claims[Claims.Email] = await _userManager.GetEmailAsync(user);
|
|
claims[Claims.EmailVerified] = await _userManager.IsEmailConfirmedAsync(user);
|
|
}
|
|
|
|
if (User.HasScope(Scopes.Phone))
|
|
{
|
|
claims[Claims.PhoneNumber] = await _userManager.GetPhoneNumberAsync(user);
|
|
claims[Claims.PhoneNumberVerified] = await _userManager.IsPhoneNumberConfirmedAsync(user);
|
|
}
|
|
|
|
if (User.HasScope("roles"))
|
|
{
|
|
claims["roles"] = await _userManager.GetRolesAsync(user);
|
|
}
|
|
|
|
// Note: the complete list of standard claims supported by the OpenID Connect specification
|
|
// can be found here: http://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
|
|
|
|
return Ok(claims);
|
|
}
|
|
}
|
|
}
|
|
|