chr*_*tok 4 asp.net-mvc windows-authentication
在 .Net MVC 应用程序中使用 Windows 身份验证时,我很难理解如何添加自定义声明。
这里的挑战是在登录时用来自数据库的自定义声明填充用户的身份,以避免每次我想检查自定义授权属性时都进行数据库调用。但是使用 Windows 身份验证对我来说使事情变得复杂,因为没有登录方法可以放置填充角色的代码。
是否有覆盖的方法,或其他方式挂钩到 Windows 身份验证登录过程?
在.NET Core 2.0 中,您应该使用 IClaimsTransformation。
例如:
public class CustomClaimsTransformer : IClaimsTransformation
{
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
{
((ClaimsIdentity)principal.Identity)
.AddClaim(new Claim(claim.Name, claim.Value)); //your values
return Task.FromResult(principal);
}
}
Run Code Online (Sandbox Code Playgroud)
并将此代码添加到 Startup.cs
...
services.AddMvc();
services.AddScoped<IClaimsTransformation,CustomClaimsTransformer>(); // Add this
Run Code Online (Sandbox Code Playgroud)
我不知道如何在 ASP.NET MVC 中执行此操作:/。
这是我在这里的第一个答案:)。
| 归档时间: |
|
| 查看次数: |
3629 次 |
| 最近记录: |