|
|
|
@ -36,6 +36,9 @@ namespace Mvc.Server { |
|
|
|
_userManager = userManager; |
|
|
|
} |
|
|
|
|
|
|
|
// Note: to support interactive flows like the code flow,
|
|
|
|
// you must provide your own authorization endpoint action:
|
|
|
|
|
|
|
|
[Authorize, HttpGet, Route("~/connect/authorize")] |
|
|
|
public async Task<IActionResult> Authorize() { |
|
|
|
// Extract the authorization request from the ASP.NET environment.
|
|
|
|
@ -121,56 +124,81 @@ namespace Mvc.Server { |
|
|
|
return SignOut(OpenIdConnectServerDefaults.AuthenticationScheme); |
|
|
|
} |
|
|
|
|
|
|
|
// Note: to support the password grant type, you must provide your own token endpoint action:
|
|
|
|
|
|
|
|
// [HttpPost("~/connect/token")]
|
|
|
|
// [Produces("application/json")]
|
|
|
|
// public async Task<IActionResult> Exchange() {
|
|
|
|
// var request = HttpContext.GetOpenIdConnectRequest();
|
|
|
|
//
|
|
|
|
// if (request.IsPasswordGrantType()) {
|
|
|
|
// var user = await _userManager.FindByNameAsync(request.Username);
|
|
|
|
// if (user == null) {
|
|
|
|
// return BadRequest(new OpenIdConnectResponse {
|
|
|
|
// Error = OpenIdConnectConstants.Errors.InvalidGrant,
|
|
|
|
// ErrorDescription = "The username/password couple is invalid."
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Ensure the password is valid.
|
|
|
|
// if (!await _userManager.CheckPasswordAsync(user, request.Password)) {
|
|
|
|
// if (_userManager.SupportsUserLockout) {
|
|
|
|
// await _userManager.AccessFailedAsync(user);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return BadRequest(new OpenIdConnectResponse {
|
|
|
|
// Error = OpenIdConnectConstants.Errors.InvalidGrant,
|
|
|
|
// ErrorDescription = "The username/password couple is invalid."
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if (_userManager.SupportsUserLockout) {
|
|
|
|
// await _userManager.ResetAccessFailedCountAsync(user);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// var identity = await _userManager.CreateIdentityAsync(user, request.GetScopes());
|
|
|
|
//
|
|
|
|
// // Create a new authentication ticket holding the user identity.
|
|
|
|
// var ticket = new AuthenticationTicket(
|
|
|
|
// new ClaimsPrincipal(identity),
|
|
|
|
// new AuthenticationProperties(),
|
|
|
|
// OpenIdConnectServerDefaults.AuthenticationScheme);
|
|
|
|
//
|
|
|
|
// ticket.SetResources(request.GetResources());
|
|
|
|
// ticket.SetScopes(request.GetScopes());
|
|
|
|
//
|
|
|
|
// return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return BadRequest(new OpenIdConnectResponse {
|
|
|
|
// Error = OpenIdConnectConstants.Errors.UnsupportedGrantType,
|
|
|
|
// ErrorDescription = "The specified grant type is not supported."
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// Note: to support non-interactive flows like password,
|
|
|
|
// you must provide your own token endpoint action:
|
|
|
|
|
|
|
|
[HttpPost("~/connect/token")] |
|
|
|
[Produces("application/json")] |
|
|
|
public async Task<IActionResult> Exchange() { |
|
|
|
var request = HttpContext.GetOpenIdConnectRequest(); |
|
|
|
|
|
|
|
if (request.IsPasswordGrantType()) { |
|
|
|
var user = await _userManager.FindByNameAsync(request.Username); |
|
|
|
if (user == null) { |
|
|
|
return BadRequest(new OpenIdConnectResponse { |
|
|
|
Error = OpenIdConnectConstants.Errors.InvalidGrant, |
|
|
|
ErrorDescription = "The username/password couple is invalid." |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure the user is allowed to sign in.
|
|
|
|
if (!await _signInManager.CanSignInAsync(user)) { |
|
|
|
return BadRequest(new OpenIdConnectResponse { |
|
|
|
Error = OpenIdConnectConstants.Errors.InvalidGrant, |
|
|
|
ErrorDescription = "The specified user is not allowed to sign in." |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// Reject the token request if two-factor authentication has been enabled by the user.
|
|
|
|
if (_userManager.SupportsUserTwoFactor && await _userManager.GetTwoFactorEnabledAsync(user)) { |
|
|
|
return BadRequest(new OpenIdConnectResponse { |
|
|
|
Error = OpenIdConnectConstants.Errors.InvalidGrant, |
|
|
|
ErrorDescription = "The specified user is not allowed to sign in." |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure the user is not already locked out.
|
|
|
|
if (_userManager.SupportsUserLockout && await _userManager.IsLockedOutAsync(user)) { |
|
|
|
return BadRequest(new OpenIdConnectResponse { |
|
|
|
Error = OpenIdConnectConstants.Errors.InvalidGrant, |
|
|
|
ErrorDescription = "The username/password couple is invalid." |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// Ensure the password is valid.
|
|
|
|
if (!await _userManager.CheckPasswordAsync(user, request.Password)) { |
|
|
|
if (_userManager.SupportsUserLockout) { |
|
|
|
await _userManager.AccessFailedAsync(user); |
|
|
|
} |
|
|
|
|
|
|
|
return BadRequest(new OpenIdConnectResponse { |
|
|
|
Error = OpenIdConnectConstants.Errors.InvalidGrant, |
|
|
|
ErrorDescription = "The username/password couple is invalid." |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
if (_userManager.SupportsUserLockout) { |
|
|
|
await _userManager.ResetAccessFailedCountAsync(user); |
|
|
|
} |
|
|
|
|
|
|
|
var identity = await _userManager.CreateIdentityAsync(user, request.GetScopes()); |
|
|
|
|
|
|
|
// Create a new authentication ticket holding the user identity.
|
|
|
|
var ticket = new AuthenticationTicket( |
|
|
|
new ClaimsPrincipal(identity), |
|
|
|
new AuthenticationProperties(), |
|
|
|
OpenIdConnectServerDefaults.AuthenticationScheme); |
|
|
|
|
|
|
|
ticket.SetResources(request.GetResources()); |
|
|
|
ticket.SetScopes(request.GetScopes()); |
|
|
|
|
|
|
|
return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme); |
|
|
|
} |
|
|
|
|
|
|
|
return BadRequest(new OpenIdConnectResponse { |
|
|
|
Error = OpenIdConnectConstants.Errors.UnsupportedGrantType, |
|
|
|
ErrorDescription = "The specified grant type is not supported." |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |