How to get Roles from using SimpleMembership?

后端 未结 2 1913
Happy的楠姐
Happy的楠姐 2021-01-21 16:19

I am developing a MVC4 application with SimpleMembership. I have a table - \"userInfo\" in which I am storing user\'s information such as Name, Email, Address, Phone, Role etc.

相关标签:
2条回答
  • 2021-01-21 16:47

    to get all available roles, assuming you have enabled Roles and added at least one..

    var roles = (SimpleRoleProvider)Roles.Provider;
    
    var allRoles = roles.GetAllRoles();
    

    to get specific user's roles.

    var userRoles = roles.GetRolesForUser("specificusername");
    

    ref MSDN

    Simple Membership does not come with any out of the box management pages for Roles. You are on your own to create them, or manage them directly through code/sql/ef etc..

    Code examples...

    Check for and creation of Admin role:

    if (!Roles.RoleExists("Admin"))
         Roles.CreateRole("Admin");
    

    Adding user to role on creation:

    if (!Roles.GetRolesForUser("specificusername").Contains("Admin"))
         Roles.AddUsersToRoles(new[] {"specificusername"}, new[] {"Admin"});
    

    ref adding-security-and-membership

    0 讨论(0)
  • 2021-01-21 16:51

    You can user Roles.GetRolesForUser Method after your user logged in

    Gets a list of the roles that the currently logged-on user is in.

    Or if you want to check whether current user is in specified role you can use Roles.IsUserInRole Method

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