因此,經調查後,我拿出2層的方法來做到這一點:
- 包括關於身份服務器端
- 包括在客戶端角色/其他索賠角色/其他索賠
包括關於標識的服務器端
拉維punjwani在「如何添加additi給出了答案onal聲明將包含在使用ASP.Net身份與IdentityServer4的access_token中。他的解決方案仍處於草案中,但該解決方案允許您在將令牌發送回客戶端之前添加任何聲明。這是鏈接:How to add additional claims to be included in the access_token using ASP.Net Identity with IdentityServer4
包括在客戶端
這一個是有點艱難,因爲它涉及到客戶端的請求管道加入「IClaimsTransformer」。結果是,每次請求,Claimstransformer將檢索用戶的聲明並將其添加到用戶標識聲明(令牌)中。 Claimstransformer的設置並不容易,因爲要讓DI工作起來很困難,但經過大量研究後,Celow解決方案可以幫助我。
自定義ClaimsTransformer類完成改造的中間件: 公共類KarekeClaimsTransformer:IClaimsTransformer { 私人只讀的UserManager _userManager;
public KarekeClaimsTransformer(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
public async Task<ClaimsPrincipal> TransformAsync(ClaimsTransformationContext context)
{
if (context.Principal.Identity.IsAuthenticated)
{
Claim userId = context.Principal.FindFirst("sub");
if (context.Principal.FindFirst("role") == null && userId != null)
{
ApplicationUser user = await _userManager.FindByIdAsync(userId.Value);
var roles = await _userManager.GetRolesAsync(user);
foreach (var role in roles)
{
((ClaimsIdentity)context.Principal.Identity).AddClaim(new Claim(JwtClaimTypes.Role, role,
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"));
}
}
}
return Task.FromResult(context.Principal).Result;
}
}
在客戶端啓動類,你需要將它添加到範圍在ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
...
services.AddScoped<IClaimsTransformer, KarekeClaimsTransformer>();
// Add framework services.
services.AddMvc();
}
最後,加入配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "http://localhost:5000",
RequireHttpsMetadata = false,
ApiName = "api1"
});
app.UseClaimsTransformation((context) =>
{
IClaimsTransformer transformer = context.Context.RequestServices.GetRequiredService<IClaimsTransformer>();
return transformer.TransformAsync(context);
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "api/{controller}/{action?}/{id?}");
});
}
可以確認角色存在在令牌?如果他們這樣做,我可能會知道是什麼導致你的痛苦。 – Lutando
不,我不能得到任何索賠(角色,定製索賠...)補充。所以我研究了很多,並找到了下面描述的兩種解決方案。我現在可以在Identity Server級別和客戶端級別添加聲明。當然,任何提出更好的建議都是值得歡迎的。 – Laobu