自定义asp.net身份角色声明

编程入门 行业动态 更新时间:2024-10-27 22:20:13
本文介绍了自定义asp身份角色声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用带有ClaimsPrincipal和ClaimsIdentity的asp身份.身份由自定义用户管理器创建,并作为承载令牌发送给客户端.

I'm using asp identity with ClaimsPrincipal and ClaimsIdentity. The identity is created by a custom usermanager and sent to the clients as a bearer token.

一切正常,但是我想指示asp使用自定义声明类型作为角色,而不是标准System.Security.Claims.ClaimTypes.Role( schemas.microsoft/ws/2008/06/identity/claims/role ).

All works fine, but I would like to instruct asp to use a custom claim type for role, instead of the standard System.Security.Claims.ClaimTypes.Role (schemas.microsoft/ws/2008/06/identity/claims/role).

有可能吗?

我想将标准的Authorize属性与角色构造函数一起使用,例如:Authorize(Roles ="staff")并让框架检查声明,但是我的自定义角色声明类型("myapproleclaim")代替了标准的.

I would like to use the standard Authorize attribute with role constructor, ex: Authorize(Roles="staff") and let the framework to check the claims, but my custom role claim type ("myapproleclaim") instead of the standard one.

推荐答案

您可以做类似的事情,

public class CustomPrinciple : ClaimsPrincipal { public CustomPrinciple(ClaimsIdentity identity) : base(identity) { } public override bool IsInRole(string role) { return HasClaim("myRoleClaimType", role); } } [TestClass] public class CustomRoleTest { [TestMethod] public void testing_custom_role_type() { var identity = new ClaimsIdentity(); identity.AddClaim(new Claim("myRoleClaimType", "role1")); var principle = new CustomPrinciple(identity); Assert.IsTrue(principle.IsInRole("role1")); Assert.IsFalse(principle.IsInRole("role2")); } }

更多推荐

自定义asp.net身份角色声明

本文发布于:2023-11-14 14:48:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   角色   声明   身份   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!