OAuthAuthorizationServerMiddleware, ASOS : .
, , ASOS beta3 ( 2015 .), offline_access , OpenID Connect: https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/issues/128
GrantResourceOwnerCredentials, ASOS :
public override async Task GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsContext context) {
string username = context.UserName;
string password = context.Password;
UserManager<ApplicationUser> userManager = context.HttpContext.RequestServices.GetRequiredService<UserManager<ApplicationUser>>();
ApplicationUser user = await userManager.FindByNameAsync(username);
if (await userManager.CheckPasswordAsync(user, password)) {
ClaimsIdentity identity = new ClaimsIdentity(
context.Options.AuthenticationScheme);
identity.AddClaim(ClaimTypes.Name, username,
OpenIdConnectConstants.Destinations.AccessToken,
OpenIdConnectConstants.Destinations.IdentityToken);
foreach (string role in await userManager.GetRolesAsync(user)) {
identity.AddClaim(ClaimTypes.Role, role,
OpenIdConnectConstants.Destinations.AccessToken,
OpenIdConnectConstants.Destinations.IdentityToken);
}
AuthenticationTicket ticket = new AuthenticationTicket(
new ClaimsPrincipal(identity),
new AuthenticationProperties(),
context.Options.AuthenticationScheme);
ticket.SetResources("resource_server_1");
List<string> scopes = new List<string>();
if (context.Request.HasScope("offline_access")) {
scopes.Add("offline_access");
}
ticket.SetScopes(scopes);
context.Validate(ticket);
} else {
context.Reject("invalid credentials");
}
return Task.FromResult(0);
}
... Angular , scope:
$http({
method: 'POST',
url: 'connect/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
data: $.param({
grant_type: 'password',
username: email,
password: password,
scope: 'offline_access'
})
}).then(function (response) {
if (response.status == 200) {
var token = response.data.access_token;
var refreshToken = response.data.refresh_token;
localStorage.setItem('token', token);
localStorage.setItem('refresh_token', refreshToken);
}
});
, refresh_token:
$http({
method: 'POST',
url: 'connect/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
data: $.param({
grant_type: 'refresh_token',
refresh_token: refreshToken
})
}).then(function (response) {
if (response.status == 200) {
var token = response.data.access_token;
localStorage.setItem('token', token);
}
});