WindowsTokenRoleProvider.IsUserInRole 方法      
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,它指示指定的用户是否扮演指定的内置 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 -> boolPublic Function IsUserInRole (username As String, role As WindowsBuiltInRole) As Boolean参数
- username
- String
要搜索的形式为 DOMAIN\username 的用户名。
- role
- WindowsBuiltInRole
作为搜索范围的 Windows 角色。
返回
如果指定的用户扮演指定的 Windows 角色,则为 true;否则为 false。
例外
              username 为 null。
当前执行用户不具有附加到 WindowsIdentity 的已验证身份的 User 对于非 HTTP 方案,当前执行用户不具有附加到 WindowsIdentity 的已验证身份的 CurrentPrincipal。
- 或 -
              username 与当前 Name 的 WindowsIdentity 不匹配。
示例
下面的代码示例以编程方式检查当前登录的用户是否为管理员角色,然后允许用户查看应用程序的角色信息。 有关启用角色管理的 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 属性强制转换为ProviderWindowsTokenRoleProvider类型来访问。
只能为当前登录的用户调用 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 -> boolPublic Overrides Function IsUserInRole (username As String, roleName As String) As Boolean参数
- username
- String
要搜索的形式为 DOMAIN\username 的用户名。
- roleName
- String
要搜索的形式为 DOMAIN\rolename 的 Windows 组。
返回
如果指定的用户名属于指定的 Windows 组,则为 true;否则为 false。
例外
当前执行用户不具有附加到 WindowsIdentity 的已验证身份的 User 对于非 HTTP 方案,当前执行用户不具有附加到 WindowsIdentity 的已验证身份的 CurrentPrincipal。
- 或 -
              username 与当前 Name 的 WindowsIdentity 不匹配。
- 或 -
在检索用户的 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>
注解
方法 IsUserInRole 由 Roles 类和 IsInRole 属性的 User 方法调用,以确定用户是否在 Windows 组中。 只能为当前登录的用户调用 IsUserInRole 方法,由 LOGON_USER 服务器变量标识。 当前登录的用户必须是经过 Windows 身份验证的用户。 有关 ASP.NET 和 Windows 身份验证的详细信息,请参阅 ASP.NET 身份验证。