ASP.NET检查ListBox中的选定用户是否处于角色中

时间:2012-01-26 03:00:42

标签: c# asp.net forms-authentication

我正在使用VS2005 C#ASP.NET。

我有一个webform,其中包含ListBox工具中的用户列表。

我想检查ListBox中所选用户是否处于特定角色,我该怎么办?

以下是我目前的代码:

if (UsersListBox.SelectedItem != null)
        {

            rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value);

            //check if the selected user is in role "Administrator"

        }

我尝试过使用this.User.IsInRole("Administrators")。但是,它只会检索活动用户的角色,而不是ListBox中的所选用户。

2 个答案:

答案 0 :(得分:2)

   if(Roles.IsUserInRole(UserListBox.SelectedItem.Value,"Administrator"))
    {
      //
    }

答案 1 :(得分:1)

if (UsersListBox.SelectedItem != null) 
{ 
    string[] rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value); 

    if (rolesArray.Contains("Administrator"))
    {
        // do something if user is Admin
    }
    else
    {
        // user is not Admin
    }
}