ASP.net is user in role

2019-04-24 16:31发布

I'm having trouble working out how to quickly find out yes/no is a username in a role? I've gotten as far as:

Roles.FindUsersInRole("Admin", usersName)

But am a bit stuck, any easy way of doing this?

标签: c# asp.net roles
2条回答
Luminary・发光体
2楼-- · 2019-04-24 16:45

There is a better way just with

if (Roles.IsUserInRole("Admin")) 
{
    // Code Here... 
}
查看更多
霸刀☆藐视天下
3楼-- · 2019-04-24 16:46

The below returns true or false depending on if the specified user is in the specified role

Roles.IsUserInRole(userName, role)

So, for example, if you wanted to remove a user from a specific role you could use

 if (Roles.IsUserInRole(userName, role))
         Roles.RemoveUserFromRole(userName, role);
查看更多
登录 后发表回答