|
|
|
@ -146,13 +146,11 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
identity.AddClaim(new Claim(Claims.Subject, identity.FindFirstValue(ClaimTypes.NameIdentifier))); |
|
|
|
identity.AddClaim(new Claim(Claims.Name, identity.FindFirstValue(ClaimTypes.Name))); |
|
|
|
|
|
|
|
var principal = new ClaimsPrincipal(identity); |
|
|
|
|
|
|
|
// Note: in this sample, the granted scopes match the requested scope
|
|
|
|
// but you may want to allow the user to uncheck specific scopes.
|
|
|
|
// For that, simply restrict the list of scopes before calling SetScopes.
|
|
|
|
principal.SetScopes(request.GetScopes()); |
|
|
|
principal.SetResources(await _scopeManager.ListResourcesAsync(principal.GetScopes()).ToListAsync()); |
|
|
|
identity.SetScopes(request.GetScopes()); |
|
|
|
identity.SetResources(await _scopeManager.ListResourcesAsync(identity.GetScopes()).ToListAsync()); |
|
|
|
|
|
|
|
// Automatically create a permanent authorization to avoid requiring explicit consent
|
|
|
|
// for future authorization or token requests containing the same scopes.
|
|
|
|
@ -160,21 +158,17 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
if (authorization == null) |
|
|
|
{ |
|
|
|
authorization = await _authorizationManager.CreateAsync( |
|
|
|
principal: principal, |
|
|
|
principal: new ClaimsPrincipal(identity), |
|
|
|
subject : user.Id, |
|
|
|
client : await _applicationManager.GetIdAsync(application), |
|
|
|
type : AuthorizationTypes.Permanent, |
|
|
|
scopes : principal.GetScopes()); |
|
|
|
scopes : identity.GetScopes()); |
|
|
|
} |
|
|
|
|
|
|
|
principal.SetAuthorizationId(await _authorizationManager.GetIdAsync(authorization)); |
|
|
|
|
|
|
|
foreach (var claim in principal.Claims) |
|
|
|
{ |
|
|
|
claim.SetDestinations(GetDestinations(claim, principal)); |
|
|
|
} |
|
|
|
identity.SetAuthorizationId(await _authorizationManager.GetIdAsync(authorization)); |
|
|
|
identity.SetDestinations(GetDestinations); |
|
|
|
|
|
|
|
context.Authentication.SignIn(new AuthenticationProperties(), (ClaimsIdentity) principal.Identity); |
|
|
|
context.Authentication.SignIn(new AuthenticationProperties(), identity); |
|
|
|
|
|
|
|
return new EmptyResult(); |
|
|
|
|
|
|
|
@ -267,13 +261,11 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
identity.AddClaim(new Claim(Claims.Subject, identity.FindFirstValue(ClaimTypes.NameIdentifier))); |
|
|
|
identity.AddClaim(new Claim(Claims.Name, identity.FindFirstValue(ClaimTypes.Name))); |
|
|
|
|
|
|
|
var principal = new ClaimsPrincipal(identity); |
|
|
|
|
|
|
|
// Note: in this sample, the granted scopes match the requested scope
|
|
|
|
// but you may want to allow the user to uncheck specific scopes.
|
|
|
|
// For that, simply restrict the list of scopes before calling SetScopes.
|
|
|
|
principal.SetScopes(request.GetScopes()); |
|
|
|
principal.SetResources(await _scopeManager.ListResourcesAsync(principal.GetScopes()).ToListAsync()); |
|
|
|
identity.SetScopes(request.GetScopes()); |
|
|
|
identity.SetResources(await _scopeManager.ListResourcesAsync(identity.GetScopes()).ToListAsync()); |
|
|
|
|
|
|
|
// Automatically create a permanent authorization to avoid requiring explicit consent
|
|
|
|
// for future authorization or token requests containing the same scopes.
|
|
|
|
@ -281,22 +273,18 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
if (authorization == null) |
|
|
|
{ |
|
|
|
authorization = await _authorizationManager.CreateAsync( |
|
|
|
principal: principal, |
|
|
|
principal: new ClaimsPrincipal(identity), |
|
|
|
subject : user.Id, |
|
|
|
client : await _applicationManager.GetIdAsync(application), |
|
|
|
type : AuthorizationTypes.Permanent, |
|
|
|
scopes : principal.GetScopes()); |
|
|
|
scopes : identity.GetScopes()); |
|
|
|
} |
|
|
|
|
|
|
|
principal.SetAuthorizationId(await _authorizationManager.GetIdAsync(authorization)); |
|
|
|
|
|
|
|
foreach (var claim in principal.Claims) |
|
|
|
{ |
|
|
|
claim.SetDestinations(GetDestinations(claim, principal)); |
|
|
|
} |
|
|
|
identity.SetAuthorizationId(await _authorizationManager.GetIdAsync(authorization)); |
|
|
|
identity.SetDestinations(GetDestinations); |
|
|
|
|
|
|
|
// Returning a SignInResult will ask OpenIddict to issue the appropriate access/identity tokens.
|
|
|
|
context.Authentication.SignIn(new AuthenticationProperties(), (ClaimsIdentity) principal.Identity); |
|
|
|
context.Authentication.SignIn(new AuthenticationProperties(), identity); |
|
|
|
|
|
|
|
return new EmptyResult(); |
|
|
|
} |
|
|
|
@ -351,11 +339,11 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
|
|
|
|
if (request.IsAuthorizationCodeGrantType() || request.IsRefreshTokenGrantType()) |
|
|
|
{ |
|
|
|
// Retrieve the claims principal stored in the authorization code/device code/refresh token.
|
|
|
|
var principal = new ClaimsPrincipal((await context.Authentication.AuthenticateAsync(OpenIddictServerOwinDefaults.AuthenticationType)).Identity); |
|
|
|
// Retrieve the claims identity stored in the authorization code/device code/refresh token.
|
|
|
|
var result = await context.Authentication.AuthenticateAsync(OpenIddictServerOwinDefaults.AuthenticationType); |
|
|
|
|
|
|
|
// Retrieve the user profile corresponding to the authorization code/refresh token.
|
|
|
|
var user = await context.GetUserManager<ApplicationUserManager>().FindByIdAsync(principal.GetClaim(Claims.Subject)); |
|
|
|
var user = await context.GetUserManager<ApplicationUserManager>().FindByIdAsync(result.Identity.GetClaim(Claims.Subject)); |
|
|
|
if (user == null) |
|
|
|
{ |
|
|
|
context.Authentication.Challenge( |
|
|
|
@ -383,16 +371,8 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
return new EmptyResult(); |
|
|
|
} |
|
|
|
|
|
|
|
var identity = new ClaimsIdentity(OpenIddictServerOwinDefaults.AuthenticationType); |
|
|
|
identity.AddClaims((await context.Get<ApplicationSignInManager>().CreateUserIdentityAsync(user)).Claims); |
|
|
|
|
|
|
|
identity.AddClaim(new Claim(Claims.Subject, identity.FindFirstValue(ClaimTypes.NameIdentifier))); |
|
|
|
identity.AddClaim(new Claim(Claims.Name, identity.FindFirstValue(ClaimTypes.Name))); |
|
|
|
|
|
|
|
foreach (var claim in identity.Claims) |
|
|
|
{ |
|
|
|
claim.SetDestinations(GetDestinations(claim, principal)); |
|
|
|
} |
|
|
|
var identity = new ClaimsIdentity(result.Identity.Claims, OpenIddictServerOwinDefaults.AuthenticationType); |
|
|
|
identity.SetDestinations(GetDestinations); |
|
|
|
|
|
|
|
// Ask OpenIddict to issue the appropriate access/identity tokens.
|
|
|
|
context.Authentication.SignIn(new AuthenticationProperties(), identity); |
|
|
|
@ -403,7 +383,7 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
throw new InvalidOperationException("The specified grant type is not supported."); |
|
|
|
} |
|
|
|
|
|
|
|
private IEnumerable<string> GetDestinations(Claim claim, ClaimsPrincipal principal) |
|
|
|
private static IEnumerable<string> GetDestinations(Claim claim) |
|
|
|
{ |
|
|
|
// Note: by default, claims are NOT automatically included in the access and identity tokens.
|
|
|
|
// To allow OpenIddict to serialize them, you must attach them a destination, that specifies
|
|
|
|
@ -414,7 +394,7 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
case Claims.Name: |
|
|
|
yield return Destinations.AccessToken; |
|
|
|
|
|
|
|
if (principal.HasScope(Scopes.Profile)) |
|
|
|
if (claim.Subject.HasScope(Scopes.Profile)) |
|
|
|
yield return Destinations.IdentityToken; |
|
|
|
|
|
|
|
yield break; |
|
|
|
@ -422,7 +402,7 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
case Claims.Email: |
|
|
|
yield return Destinations.AccessToken; |
|
|
|
|
|
|
|
if (principal.HasScope(Scopes.Email)) |
|
|
|
if (claim.Subject.HasScope(Scopes.Email)) |
|
|
|
yield return Destinations.IdentityToken; |
|
|
|
|
|
|
|
yield break; |
|
|
|
@ -430,7 +410,7 @@ namespace OpenIddict.Sandbox.AspNet.Server.Controllers |
|
|
|
case Claims.Role: |
|
|
|
yield return Destinations.AccessToken; |
|
|
|
|
|
|
|
if (principal.HasScope(Scopes.Roles)) |
|
|
|
if (claim.Subject.HasScope(Scopes.Roles)) |
|
|
|
yield return Destinations.IdentityToken; |
|
|
|
|
|
|
|
yield break; |
|
|
|
|