Mar*_*k G 5 bearer-token openid-connect asp.net-identity-3 aspnet-contrib asp.net-core
基于Shaun Luttin在/sf/answers/2160026711/上的一个很好的例子,我能够使用该代码生成和使用承载令牌.微小的变化是获得最新的包:
"dependencies": {
"Microsoft.AspNet.Authentication.JwtBearer": "1.0.0-rc1-final",
"AspNet.Security.OpenIdConnect.Server": "1.0.0-beta4"
}
Run Code Online (Sandbox Code Playgroud)
虽然代码是一个很好的开始,但它并不是一个完全集成w/ASP.NET身份的完整解决方案.我修改了AuthorizationProvider类,如下所示:
public override Task GrantResourceOwnerCredentials(
GrantResourceOwnerCredentialsContext context)
{
var user = _userManager.FindByNameAsync(context.UserName).Result;
if (user == null)
{
context.Rejected("The user name or password is incorrect.");
}
else
{
var signInManager = context.HttpContext.RequestServices
.GetRequiredService<SignInManager<ApplicationUser>>();
if (signInManager.CanSignInAsync(user).Result &&
_userManager.CheckPasswordAsync(user, context.Password).Result)
{
var principal = signInManager.CreateUserPrincipalAsync(user).Result;
//To avoid leaking confidential data, AspNet.Security.OpenIdConnect.Server
//refuses to serialize the claims that don't explicitly specify a destination.
foreach (var claim in principal.Claims)
claim.WithDestination("token id_token");
context.Validated(principal);
}
else
context.Rejected("The user name or password is incorrect.");
}
return Task.FromResult(0);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用CreateUserPrincipalAsync为Validated方法创建ClaimsPrincipal.是否有更好的方法来集成w/ASP.NET身份?
你的实现看起来不错,有 3 点小注释:
async/await以避免.Result阻塞调用。最后两点在OpenIddict(内部使用的全新实验性 OIDC 服务器AspNet.Security.OpenIdConnect.Server)中得到了缓解,因此请毫不犹豫地查看其默认实现: https: //github.com/openiddict/core/blob/dev/src /OpenIddict.Core/OpenIddictProvider.cs#L353。
| 归档时间: |
|
| 查看次数: |
1103 次 |
| 最近记录: |