WindowsTokenRoleProvider.IsUserInRole 方法

定义

获取一个值,它指示指定的用户是否扮演指定的内置 Windows 角色。

重载

IsUserInRole(String, WindowsBuiltInRole)

获取一个值,它指示指定的用户是否扮演指定的内置 Windows 角色。

IsUserInRole(String, String)

获取一个值,它指示指定的用户是否属于指定的 Windows 组。

IsUserInRole(String, WindowsBuiltInRole)

获取一个值,它指示指定的用户是否扮演指定的内置 Windows 角色。

public:
 bool IsUserInRole(System::String ^ username, System::Security::Principal::WindowsBuiltInRole role);
public bool IsUserInRole (string username, System.Security.Principal.WindowsBuiltInRole role);
override this.IsUserInRole : string * System.Security.Principal.WindowsBuiltInRole -> bool
Public Function IsUserInRole (username As String, role As WindowsBuiltInRole) As Boolean

参数

username
String

要搜索的形式为 DOMAIN\username 的用户名。

role
WindowsBuiltInRole

作为搜索范围的 Windows 角色。

返回

Boolean

如果指定的用户扮演指定的 Windows 角色,则为 true;否则为 false

例外

usernamenull

当前执行用户不具有附加到 WindowsIdentity 的已验证身份的 User 对于非 HTTP 方案,当前执行用户不具有附加到 WindowsIdentity 的已验证身份的 CurrentPrincipal

  • 或 -

username 与当前 NameWindowsIdentity 不匹配。

示例

下面的代码示例以编程方式检查当前登录的用户是否在管理员角色中,然后允许用户查看应用程序的角色信息。 有关启用角色管理的Web.config文件的示例,请参阅 WindowsTokenRoleProvider

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

string[] rolesArray;

public void Page_Load()
{
  Msg.Text = "";

  WindowsPrincipal p = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;

  if (!p.IsInRole(WindowsBuiltInRole.Administrator))
  {
    Msg.Text = "You are not authorized to view user roles.";
    return;
  }


  // Bind roles to GridView.

  try
  {
    rolesArray = Roles.GetRolesForUser(User.Identity.Name);
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }

  UserRolesGrid.DataSource = rolesArray;
  UserRolesGrid.DataBind();

  UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3>View User Roles</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" 
                                     AutoGenerateColumns="false" Gridlines="None" 
                                     CellSpacing="0" >
                         <HeaderStyle BackColor="navy" ForeColor="white" />
                         <Columns>
                           <asp:TemplateField HeaderText="Roles" >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Dim rolesArray() As String

Public Sub Page_Load()
  Msg.Text = ""

  Dim provider As WindowsTokenRoleProvider = CType(Roles.Provider, WindowsTokenRoleProvider)

  If Not provider.IsUserInRole(User.Identity.Name, _
                               System.Security.Principal.WindowsBuiltInRole.Administrator) Then
    Msg.Text = "You are not authorized to view user roles."
    Return
  End If


  ' Bind roles to GridView.

  Try
    rolesArray = Roles.GetRolesForUser(User.Identity.Name)
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try

  UserRolesGrid.DataSource = rolesArray
  UserRolesGrid.DataBind()

  UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3>View User Roles</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" 
                                     AutoGenerateColumns="false" Gridlines="None" 
                                     CellSpacing="0" >
                         <HeaderStyle BackColor="navy" ForeColor="white" />
                         <Columns>
                           <asp:TemplateField HeaderText="Roles" >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>

注解

使用此方法IsUserInRole可以检查用户是否位于枚举描述WindowsBuiltInRole的常见Windows角色之一。 此方法适用于本地化为多种语言的应用程序。 此方法的IsUserInRole此重载不是基类的RoleProvider一部分,只能通过将类的属性Roles强制Provider转换为WindowsTokenRoleProvider类型来访问。

只能 IsUserInRole 调用当前登录用户的方法,由LOGON_USER服务器变量标识。 如果参数中 username 提供的值不是当前登录用户的名称,则会引发一个 HttpException

IsUserInRole 只能为由LOGON_USER服务器变量标识的当前登录用户调用方法。 当前登录的用户必须是经过身份验证Windows的用户。 有关 ASP.NET 和Windows 身份验证的详细信息,请参阅 ASP.NET 身份验证

另请参阅

适用于

IsUserInRole(String, String)

获取一个值,它指示指定的用户是否属于指定的 Windows 组。

public:
 override bool IsUserInRole(System::String ^ username, System::String ^ roleName);
public override bool IsUserInRole (string username, string roleName);
override this.IsUserInRole : string * string -> bool
Public Overrides Function IsUserInRole (username As String, roleName As String) As Boolean

参数

username
String

要搜索的形式为 DOMAIN\username 的用户名。

roleName
String

要搜索的形式为 DOMAIN\rolename 的 Windows 组。

返回

Boolean

如果指定的用户名属于指定的 Windows 组,则为 true;否则为 false

例外

usernamenull

  • 或 -

roleNamenull

当前执行用户不具有附加到 WindowsIdentity 的已验证身份的 User 对于非 HTTP 方案,当前执行用户不具有附加到 WindowsIdentity 的已验证身份的 CurrentPrincipal

  • 或 -

username 与当前 NameWindowsIdentity 不匹配。

  • 或 -

在检索用户的 Windows 组信息时发生错误。

示例

下面的代码示例以编程方式检查当前登录的用户是否在管理员角色中,然后允许用户查看应用程序的角色信息。 有关启用角色管理的Web.config文件的示例,请参阅 WindowsTokenRoleProvider

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

string[] rolesArray;

public void Page_Load()
{
  Msg.Text = "";

  try
  {
    if (!Roles.IsUserInRole(User.Identity.Name, @"BUILTIN\Administrators"))
    {
      Msg.Text = "You are not authorized to view user roles.";
      return;
    }
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }


  // Bind roles to GridView.

  rolesArray = Roles.GetRolesForUser(User.Identity.Name);
  UserRolesGrid.DataSource = rolesArray;
  UserRolesGrid.DataBind();

  UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3>View User Roles</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" 
                                     AutoGenerateColumns="false" Gridlines="None" 
                                     CellSpacing="0" >
                         <HeaderStyle BackColor="navy" ForeColor="white" />
                         <Columns>
                           <asp:TemplateField HeaderText="Roles" >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Dim rolesArray() As String

Public Sub Page_Load()
  Msg.Text = ""

  Try
    If Not Roles.IsUserInRole(User.Identity.Name, "BUILTIN\Administrators") Then
      Msg.Text = "You are not authorized to view user roles."
      Return
    End If
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try

  ' Bind roles to GridView.

  rolesArray = Roles.GetRolesForUser(User.Identity.Name)
  UserRolesGrid.DataSource = rolesArray
  UserRolesGrid.DataBind()

  UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>

<form runat="server" id="PageForm">

  <h3>View User Roles</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  <table border="0" cellspacing="4">
    <tr>
      <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" 
                                     AutoGenerateColumns="false" Gridlines="None" 
                                     CellSpacing="0" >
                         <HeaderStyle BackColor="navy" ForeColor="white" />
                         <Columns>
                           <asp:TemplateField HeaderText="Roles" >
                             <ItemTemplate>
                               <%# Container.DataItem.ToString() %>
                             </ItemTemplate>
                           </asp:TemplateField>
                         </Columns>
                       </asp:GridView></td>
    </tr>
  </table>

</form>

</body>
</html>

注解

该方法IsUserInRoleRoles类和IsInRole属性的方法User调用,以确定用户是否位于Windows组中。 只能 IsUserInRole 调用当前登录用户的方法,由LOGON_USER服务器变量标识。 当前登录用户必须是经过身份验证Windows的用户。 有关 ASP.NET 和Windows 身份验证的详细信息,请参阅 ASP.NET 身份验证

另请参阅

适用于