6 changed files with 150 additions and 81 deletions
@ -0,0 +1,58 @@ |
|||
using System.Threading.Tasks; |
|||
using AspNet.Security.OAuth.Validation; |
|||
using AspNet.Security.OpenIdConnect.Primitives; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Mvc.Server.Models; |
|||
using Newtonsoft.Json.Linq; |
|||
using OpenIddict.Core; |
|||
|
|||
namespace Mvc.Server.Controllers { |
|||
[Route("api")] |
|||
public class UserinfoController : Controller { |
|||
private readonly UserManager<ApplicationUser> _userManager; |
|||
|
|||
public UserinfoController(UserManager<ApplicationUser> userManager) { |
|||
_userManager = userManager; |
|||
} |
|||
|
|||
//
|
|||
// GET: /api/userinfo
|
|||
[Authorize(ActiveAuthenticationSchemes = OAuthValidationDefaults.AuthenticationScheme)] |
|||
[HttpGet("userinfo"), Produces("application/json")] |
|||
public async Task<IActionResult> Userinfo() { |
|||
var user = await _userManager.GetUserAsync(User); |
|||
if (user == null) { |
|||
return BadRequest(new OpenIdConnectResponse { |
|||
Error = OpenIdConnectConstants.Errors.InvalidGrant, |
|||
ErrorDescription = "The user profile is no longer available." |
|||
}); |
|||
} |
|||
|
|||
var claims = new JObject(); |
|||
|
|||
// Note: the "sub" claim is a mandatory claim and must be included in the JSON response.
|
|||
claims[OpenIdConnectConstants.Claims.Subject] = await _userManager.GetUserIdAsync(user); |
|||
|
|||
if (User.HasClaim(OpenIdConnectConstants.Claims.Scope, OpenIdConnectConstants.Scopes.Email)) { |
|||
claims[OpenIdConnectConstants.Claims.Email] = await _userManager.GetEmailAsync(user); |
|||
claims[OpenIdConnectConstants.Claims.EmailVerified] = await _userManager.IsEmailConfirmedAsync(user); |
|||
} |
|||
|
|||
if (User.HasClaim(OpenIdConnectConstants.Claims.Scope, OpenIdConnectConstants.Scopes.Phone)) { |
|||
claims[OpenIdConnectConstants.Claims.PhoneNumber] = await _userManager.GetPhoneNumberAsync(user); |
|||
claims[OpenIdConnectConstants.Claims.PhoneNumberVerified] = await _userManager.IsPhoneNumberConfirmedAsync(user); |
|||
} |
|||
|
|||
if (User.HasClaim(OpenIdConnectConstants.Claims.Scope, OpenIddictConstants.Scopes.Roles)) { |
|||
claims[OpenIddictConstants.Claims.Roles] = JArray.FromObject(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 Json(claims); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,40 @@ |
|||
using System; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.AspNetCore.Http; |
|||
|
|||
namespace Mvc.Server.Extensions { |
|||
public static class AppBuilderExtensions { |
|||
public static IApplicationBuilder UseWhen(this IApplicationBuilder app, |
|||
Func<HttpContext, bool> condition, Action<IApplicationBuilder> configuration) { |
|||
if (app == null) { |
|||
throw new ArgumentNullException(nameof(app)); |
|||
} |
|||
|
|||
if (condition == null) { |
|||
throw new ArgumentNullException(nameof(condition)); |
|||
} |
|||
|
|||
if (configuration == null) { |
|||
throw new ArgumentNullException(nameof(configuration)); |
|||
} |
|||
|
|||
var builder = app.New(); |
|||
configuration(builder); |
|||
|
|||
return app.Use(next => { |
|||
builder.Run(next); |
|||
|
|||
var branch = builder.Build(); |
|||
|
|||
return context => { |
|||
if (condition(context)) { |
|||
return branch(context); |
|||
} |
|||
|
|||
return next(context); |
|||
}; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue