我可以利用user.IsInRole,而无需使用会员? [英] can I make use of user.IsInRole without using Membership?
问题描述
我使用窗体身份验证,我想利用角色,可我不知怎么设置用户的角色,而不会员?
I'm using Forms authentication and I would like to make use of roles, can I somehow set the role of the user without Membership ?
推荐答案
一个简单的方法来做到这一点是角色列表存储在身份验证票据,当用户进行身份验证。那么对于每一个请求( Application_AuthenticateRequest
Global.asax文件的方法),你提取的角色,将它们添加到的GenericPrincipal
对象,并设置 HttpContext.User中
属性。
A simple way to do it is to store the list of roles in the authentication ticket when the user is authenticated. Then for every request (Application_AuthenticateRequest
method of the global.asax file) you extract the roles, add them to a GenericPrincipal
object and set the Httpcontext.User
property.
您 User.IsInRole(角色)
和 [AuthorizeAttribute(角色=角色)]
将然后恢复正常。
请参阅this回答为code详细介绍了如何做到这一点。
See this answer for code detailing how to do it.
这篇关于我可以利用user.IsInRole,而无需使用会员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!