asp.net-mvc – 如何将ASP.net身份角色放入Identityserver4身份令牌
|
虽然我很高兴IdentityServer4存在并且在许多方面使我在身份验证方面的生活变得更加容易,但我偶然发现了问题以及在社区中为声明添加角色的许多讨论. 我的要求很简单: >我开发需要身份验证和授权的应用程序(xamarin表单) 我花了无数个小时尝试不同的配置,以便在认证后将asp.net Identity角色传递给我的MVC应用程序,但没有运气.目的如第6点所述. 我也花了无数个小时阅读,但我感觉自版本v1.0.0以来,角色的实现IdentityServer4.AspIdentity已经发生了很大的变化. 在阅读了很多关于这一点后,仍然不清楚如何实际实现这一点,因为似乎仅在2个月前描述的一些解决方案不再有效. 所以,现在我相信有两条路: >仅通过请求身份令牌将角色置于身份令牌中 无论如何,这是我的假设. 那么,请详细帮助解释/记录,以便我们能够以持久的方式开始实施吗?一些可行的例子也很棒. 解决方法因此,经过调查,我提出了两种方法来做到这一点:>在Identity Server端包含角色/其他声明 包含在Identity Server端 ravi punjwani在“如何使用带有IdentityServer4的ASP.Net标识添加要包含在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解决方案为我做了这件事. Custom ClaimsTransformer类在中间件中进行转换: 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;
}
}
在Client启动类中,您需要将其添加到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?}");
});
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-5 – 什么是最新的ASP .NET MVC版本?
- asp.net-mvc – 在Razor VB.net中使用MVC无法按预期工作
- asp.net-mvc – 在MVC4中绑定的正确方法
- asp.net – SQL Reporting Services – 打印按钮未显示在Mo
- asp.net-mvc – 图像优化框架未初始化
- ASP.NET Forms Auth当所有其他应该被拒绝时,允许访问子目录
- 将自定义属性添加到asp.NET RadioButton控件
- ASP.NET页面验证
- asp.net-mvc – ASP.NET MVC是否使用常规工具箱控件?
- asp.net-mvc-4 – 我应该如何使用ReturnUrl = ViewBag.Retu
- asp.net – Web API将OAuth令牌作为XML返回
- asp.net-mvc – MVC4脚手架添加控制器给出错误“
- asp.net-mvc – ASP.NET MVC:在其中生成带有自定
- 如何从ASP.NET Web服务生成JSONP以进行跨域调用?
- asp.net-mvc-3 – jQuery.validator.unobtrusive
- 在ASP.Net Web应用程序中运行后台任务并获得反馈
- asp.net-mvc – 如何创建ASP.NET MVC区域作为插件
- asp.net-mvc – MVC DateTime文本框格式化问题
- asp.net – 无法加载类型’site._Default[已关闭
- ASP.NET MVC 5(VS2013 final):使用OWIN进行Face
