Share via


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 角色。

傳回

如果指定使用者在指定的 Windows 角色中,則為 true,否則為 false

例外狀況

usernamenull

目前正在執行的使用者,沒有將已驗證的 WindowsIdentity 附加至 User。 在非 HTTP 案例中,目前正在執行的使用者沒有將已驗證的 WindowsIdentity 附加至 CurrentPrincipal

-或-

username 不符合目前 NameWindowsIdentity

範例

下列程式碼範例會以程式設計方式檢查目前登入的使用者是否在 Administrators 角色中,再允許使用者檢視應用程式的角色資訊。 如需啟用角色管理的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 -> bool
Public Overrides Function IsUserInRole (username As String, roleName As String) As Boolean

參數

username
String

要搜尋的使用者名稱,格式為 DOMAIN\username。

roleName
String

要搜尋的 Windows 群組,格式為 DOMAIN\rolename。

傳回

如果指定使用者名稱在指定的 Windows 群組中,則為 true,否則為 false

例外狀況

usernamenull

-或-

roleNamenull

目前正在執行的使用者,沒有將已驗證的 WindowsIdentity 附加至 User。 在非 HTTP 案例中,目前正在執行的使用者沒有將已驗證的 WindowsIdentity 附加至 CurrentPrincipal

-或-

username 不符合目前 NameWindowsIdentity

-或-

擷取使用者的 Windows 群組資訊時失敗。

範例

下列程式碼範例會以程式設計方式檢查目前登入的使用者是否在 Administrators 角色中,再允許使用者檢視應用程式的角色資訊。 如需啟用角色管理的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 驗證

另請參閱

適用於