WindowsTokenRoleProvider.GetRolesForUser(String) Metoda

Definicja

Pobiera listę grup Windows, w których znajduje się użytkownik.

public:
 override cli::array <System::String ^> ^ GetRolesForUser(System::String ^ username);
public override string[] GetRolesForUser (string username);
override this.GetRolesForUser : string -> string[]
Public Overrides Function GetRolesForUser (username As String) As String()

Parametry

username
String

Użytkownik, który zwróci listę grup Windows dla w postaci DOMENA\nazwa użytkownika.

Zwraca

String[]

Tablica ciągów zawierająca nazwy wszystkich grup Windows, w których znajduje się określony użytkownik.

Wyjątki

Aktualnie wykonywany użytkownik nie ma uwierzytelnionego WindowsIdentity dołączonego elementu do usługi User. W przypadku scenariuszy innych niż HTTP aktualnie wykonywany użytkownik nie ma uwierzytelnionego WindowsIdentity dołączonego elementu do usługi CurrentPrincipal.

-lub-

usernameparametr nie jest zgodny z bieżącym WindowsIdentityelementem Name .

-lub-

Wystąpił błąd podczas pobierania informacji o grupie Windows użytkownika.

username to null.

Poziom zaufania jest mniejszy niż Low.

Przykłady

W poniższym przykładzie kodu użyto GetRolesForUser metody , aby pobrać listę ról dla określonego użytkownika i powiązać listę ról z kontrolką GridView . Aby zapoznać się z przykładem pliku Web.config umożliwiającego zarządzanie rolami, zobacz 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>

Uwagi

Ta metoda jest wywoływana przez klasę Roles w celu pobrania z systemu operacyjnego Windows listy grup Windows, w których znajduje się określony użytkownik. Metodę GetRolesForUser można wywołać tylko dla aktualnie zalogowanego użytkownika, zidentyfikowanego przez zmienną serwera LOGON_USER. Jeśli wartość podana w parametrze username nie jest nazwą aktualnie zalogowanego użytkownika, System.Configuration.Provider.ProviderException jest zgłaszana wartość .

Aby uzyskać więcej informacji na temat uwierzytelniania ASP.NET i Windows, zobacz ASP.NET Authentication (Uwierzytelnianie ASP.NET).

Dotyczy

Zobacz też