Roles.IsUserInRole 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个指示用户是否属于指定角色的值。
重载
| IsUserInRole(String) | 获取一个值,该值指示当前登录的用户是否属于指定的角色。 API 只能在 ASP.NET 请求线程的上下文内进行调用,在该批准的使用情况下,它是线程安全的。 | 
| IsUserInRole(String, String) | 获取一个指示指定用户是否属于指定角色的值。 API 只能在 ASP.NET 请求线程的上下文内进行调用,在该批准的使用情况下,它是线程安全的。 | 
IsUserInRole(String)
获取一个值,该值指示当前登录的用户是否属于指定的角色。 API 只能在 ASP.NET 请求线程的上下文内进行调用,在该批准的使用情况下,它是线程安全的。
public:
 static bool IsUserInRole(System::String ^ roleName);public static bool IsUserInRole (string roleName);static member IsUserInRole : string -> boolPublic Shared Function IsUserInRole (roleName As String) As Boolean参数
- roleName
- String
要搜索的角色的名称。
返回
如果当前登录的用户属于指定的角色,则为 true;否则为 false。
例外
              roleName 为空字符串或者包含逗号 (,)。
未启用角色管理。
示例
下面的代码示例以编程方式检查当前登录的用户是否处于管理员角色,然后允许用户查看应用程序的角色设置。 有关启用角色管理的 Web.config 文件的示例,请参阅 Roles。
<%@ 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;
MembershipUserCollection users;
public void Page_Load()
{
  Msg.Text = "";
  try
  {
    if (!Roles.IsUserInRole(User.Identity.Name, "Administrators"))
    {
      Msg.Text = "You are not authorized to view user roles.";
      UsersListBox.Visible = false;
      return;
    }
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }
  if (!IsPostBack)
  {
    // Bind users to ListBox.
    users = Membership.GetAllUsers();
    UsersListBox.DataSource = users;
    UsersListBox.DataBind();
  }
  // If a user is selected, show the roles for the selected user.
  if (UsersListBox.SelectedItem != null)
  {
    // Bind roles to GridView.
    rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value);
    UserRolesGrid.DataSource = rolesArray;
    UserRolesGrid.DataBind();
    UserRolesGrid.Columns[0].HeaderText = "Roles for " + UsersListBox.SelectedItem.Value;
  }
}
</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:ListBox id="UsersListBox" DataTextField="Username" 
                                    Rows="8" AutoPostBack="true" runat="server" /></td>
      <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
Dim users As MembershipUserCollection
Public Sub Page_Load()
  Msg.Text = ""
  Try
    If Not Roles.IsUserInRole(User.Identity.Name, "Administrators") Then
      Msg.Text = "You are not authorized to view user roles."
      UsersListBox.Visible = False
      Return
    End If
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try
  If Not IsPostBack Then
    ' Bind users to ListBox.
    users = Membership.GetAllUsers()
    UsersListBox.DataSource = users
    UsersListBox.DataBind()
  End If
  ' If a user is selected, show the roles for the selected user.
  If Not UsersListBox.SelectedItem Is Nothing Then
    ' Bind roles to GridView.
    rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value)
    UserRolesGrid.DataSource = rolesArray
    UserRolesGrid.DataBind()
    UserRolesGrid.Columns(0).HeaderText = "Roles for " & UsersListBox.SelectedItem.Value
  End If
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:ListBox id="UsersListBox" DataTextField="Username" 
                                    Rows="8" AutoPostBack="true" runat="server" /></td>
      <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 调用 RoleProvider.IsUserInRole 默认角色提供程序的 方法,以确定当前登录的用户是否与数据源中在 属性中指定的 ApplicationName 应用程序的角色相关联。 当前登录的用户由 HttpContext.User 当前 System.Web.HttpContext的 属性标识,或者由非 HTTP 托管环境标识 Thread.CurrentPrincipal 。 如果没有用户登录,则会引发异常。 仅检索属性中指定的 ApplicationName 应用程序的角色。
如果 CacheRolesInCookie 为 true,则可以 roleName 针对角色缓存而不是指定的角色提供程序进行检查。
另请参阅
适用于
IsUserInRole(String, String)
获取一个指示指定用户是否属于指定角色的值。 API 只能在 ASP.NET 请求线程的上下文内进行调用,在该批准的使用情况下,它是线程安全的。
public:
 static bool IsUserInRole(System::String ^ username, System::String ^ roleName);public static bool IsUserInRole (string username, string roleName);static member IsUserInRole : string * string -> boolPublic Shared Function IsUserInRole (username As String, roleName As String) As Boolean参数
- username
- String
要搜索的用户的名称。
- roleName
- String
要搜索的角色的名称。
返回
如果指定的用户属于指定角色,则为 true;否则为 false。
例外
未启用角色管理。
示例
下面的代码示例以编程方式检查用户是否处于管理员角色,然后允许用户查看应用程序的角色设置。 有关启用角色管理的 Web.config 文件的示例,请参阅 Roles。
<%@ 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;
MembershipUserCollection users;
public void Page_Load()
{
  Msg.Text = "";
  try
  {
    if (!Roles.IsUserInRole(User.Identity.Name, "Administrators"))
    {
      Msg.Text = "You are not authorized to view user roles.";
      UsersListBox.Visible = false;
      return;
    }
  }
  catch (HttpException e)
  {
    Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
    return;
  }
  if (!IsPostBack)
  {
    // Bind users to ListBox.
    users = Membership.GetAllUsers();
    UsersListBox.DataSource = users;
    UsersListBox.DataBind();
  }
  // If a user is selected, show the roles for the selected user.
  if (UsersListBox.SelectedItem != null)
  {
    // Bind roles to GridView.
    rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value);
    UserRolesGrid.DataSource = rolesArray;
    UserRolesGrid.DataBind();
    UserRolesGrid.Columns[0].HeaderText = "Roles for " + UsersListBox.SelectedItem.Value;
  }
}
</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:ListBox id="UsersListBox" DataTextField="Username" 
                                    Rows="8" AutoPostBack="true" runat="server" /></td>
      <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
Dim users As MembershipUserCollection
Public Sub Page_Load()
  Msg.Text = ""
  Try
    If Not Roles.IsUserInRole(User.Identity.Name, "Administrators") Then
      Msg.Text = "You are not authorized to view user roles."
      UsersListBox.Visible = False
      Return
    End If
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified."
    Return
  End Try
  If Not IsPostBack Then
    ' Bind users to ListBox.
    users = Membership.GetAllUsers()
    UsersListBox.DataSource = users
    UsersListBox.DataBind()
  End If
  ' If a user is selected, show the roles for the selected user.
  If Not UsersListBox.SelectedItem Is Nothing Then
    ' Bind roles to GridView.
    rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value)
    UserRolesGrid.DataSource = rolesArray
    UserRolesGrid.DataBind()
    UserRolesGrid.Columns(0).HeaderText = "Roles for " & UsersListBox.SelectedItem.Value
  End If
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:ListBox id="UsersListBox" DataTextField="Username" 
                                    Rows="8" AutoPostBack="true" runat="server" /></td>
      <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 调用 IsUserInRole 默认角色提供程序的 方法,以确定用户名是否与数据源中在 属性中指定的 ApplicationName 应用程序的角色相关联。
如果 username 等于当前登录用户且 CacheRolesInCookie 属性值为 true, roleName 则可能会针对角色缓存而不是指定的 Provider进行检查。