User.IsInRole doesn't work

后端 未结 16 1355
北恋
北恋 2020-12-05 18:29

I have ASP.NET MVC 4 application. I use Simple Membership Provider allowing to tick remember me checkbox under login form. If ticked, persitent cookie .ASPXAUTH is created w

相关标签:
16条回答
  • 2020-12-05 19:01

    I had a similar issue, seems like authentication module is not wired up completely with the SimpleMembershipProvider. To get around that problem, you can use a Role class to access needed information. For example to check if the use in role you can use the following:

    Roles.GetRolesForUser("sergey").Contains("Developer")
    

    And so other useful methods when working wit SimpleMembershipProvider roles:

       Roles.RoleExists("Developer")
       Roles.CreateRole("Developer");
       Roles.AddUserToRole("sergey", "Developer");
    
    0 讨论(0)
  • 2020-12-05 19:02

    Quite old topic but can confirm in vs 2015 Web forms application works solution:

    <modules>
        <remove name="FormsAuthenticationModule" />
        <remove name="RoleManager" /> <!--add this to web config-->
    </modules>
    
    0 讨论(0)
  • 2020-12-05 19:03

    For my code it had to be plural, not singular:

    User.IsInRole("Administrators")
    
    0 讨论(0)
  • 2020-12-05 19:05

    I had a similar problem with IsUserInRole returning false. By placing a watch on it, I was able to overcome the problem by using the overload described below. Try it, put a break point where you're getting false, and see what happens.

    @if (Roles.IsUserInRole(Model.UserName, "Administrator"))
    

    I'm pretty sure you could also use User.Identity.Name as the first parameter.

    0 讨论(0)
提交回复
热议问题