【发布时间】:2016-04-30 20:48:21
【问题描述】:
我有一个使用 asp.net vnext 的 angularJS 应用程序,它使用 JwtBearerAuthentication 进行身份验证。为了验证我使用AspNet.Security.OpenIdConnect.Server 的应用程序。当我登录时,我收到一个 json 响应,其中包含可用于授权请求的 access_token。我也想收到刷新令牌。这怎么可能?
Startup.cs
public void Configure(IApplicationBuilder app) {
app.UseJwtBearerAuthentication(options => {
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.TokenValidationParameters.ValidateAudience = false;
options.Authority = Configuration.Get<string>("OAuth:Authority");
options.ConfigurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(
metadataAddress: options.Authority + ".well-known/openid-configuration",
configRetriever: new OpenIdConnectConfigurationRetriever(),
docRetriever: new HttpDocumentRetriever() { RequireHttps = false });
});
app.UseOpenIdConnectServer(configuration => {
configuration.Issuer = new Uri(Configuration.Get<string>("OpenId:Issuer"));
configuration.AllowInsecureHttp = true;
configuration.AuthorizationEndpointPath = PathString.Empty;
configuration.AuthenticationScheme = OpenIdConnectServerDefaults.AuthenticationScheme;
configuration.Provider = new AuthorizationProvider();
});
}
AuthorizationProvider.cs
public class AuthorizationProvider : OpenIdConnectServerProvider {
public override Task ValidateClientAuthentication(ValidateClientAuthenticationContext context) {
context.Skipped();
return Task.FromResult<object>(null);
}
public override Task GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsContext context) {
string username = context.UserName;
string password = context.Password;
UserManager<ApplicationUser> userManager = context.HttpContext.RequestServices.GetRequiredService<UserManager<ApplicationUser>>();
ApplicationUser user = userManager.FindByNameAsync(username).Result;
if (userManager.CheckPasswordAsync(user, password).Result) {
ClaimsIdentity identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
identity.AddClaim(ClaimTypes.Name, username, "token id_token");
List<string> roles = userManager.GetRolesAsync(user).Result.ToList();
foreach (string role in roles) {
identity.AddClaim(ClaimTypes.Role, role, "token id_token");
}
ClaimsPrincipal principal = new ClaimsPrincipal(identity);
context.Validated(principal);
} else {
context.Rejected("invalid credentials");
}
return Task.FromResult<object>(null);
}
}
AngularJS 登录代码
$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
})
}).then(function (response) {
if (response.status == 200) {
var token = response.data.access_token;
localStorage.setItem('token', token);
}
});
【问题讨论】:
-
附带说明,您不应禁用受众验证 (
options.TokenValidationParameters.ValidateAudience = false)。相反,请考虑使用ticket.SetResources(new[] { "resource_server_1" })在访问令牌中设置正确的受众并在您的 JWT 不记名中间件选项中进行配置:options.Audience = "resource_server_1"。 -
你让我免于问另一个问题。我会试试看。谢谢
标签: asp.net angularjs asp.net-core jwt aspnet-contrib