can I make use of user.IsInRole without using Membership?

后端 未结 4 1353
情深已故
情深已故 2021-01-14 15:52

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 ?

4条回答
  •  执笔经年
    2021-01-14 16:42

    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.

    Your User.IsInRole("role") and [AuthorizeAttribute(Roles="role")] will then work as normal.

    See this answer for code detailing how to do it.

提交回复
热议问题