WindowsTokenRoleProvider.GetRolesForUser(String) Yöntem

Tanım

Kullanıcının içinde olduğu Windows gruplarının listesini alır.

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()

Parametreler

username
String

Windows gruplarının listesini ETKİ ALANI\kullanıcıadı biçiminde döndürecek kullanıcı.

Döndürülenler

String[]

Belirtilen kullanıcının bulunduğu tüm Windows gruplarının adlarını içeren bir dize dizisi.

Özel durumlar

Şu anda yürütülmekte olan kullanıcının bağlı WindowsIdentitykimliği doğrulanmış User bir kullanıcısı yok. HTTP olmayan senaryolarda, şu anda yürütülen kullanıcının bağlı WindowsIdentitykimliği doğrulanmış CurrentPrincipal bir kullanıcısı yoktur.

-veya-

usernamegeçerli Nameile WindowsIdentity eşleşmiyor.

-veya-

Kullanıcının Windows grup bilgileri alınırken bir hata oluştu.

username, null'e eşittir.

Güven düzeyi değerinden Lowküçüktür.

Örnekler

Aşağıdaki kod örneği, belirtilen kullanıcının rol listesini almak için yöntemini kullanır GetRolesForUser ve rol listesini bir GridView denetime bağlar. Rol yönetimini etkinleştiren bir Web.config dosyası örneği için bkz 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>

Açıklamalar

Bu yöntem, belirtilen kullanıcının içinde olduğu Windows gruplarının listesini Windows işletim sisteminden almak için Roles sınıfı tarafından çağrılır. GetRolesForUser yöntemi yalnızca LOGON_USER sunucu değişkeni tarafından tanımlanan oturum açmış olan kullanıcı için çağrılabilir. parametresinde username sağlanan değer şu anda oturum açmış olan kullanıcının adı değilse, bir System.Configuration.Provider.ProviderException oluşturulur.

ASP.NET ve Windows authentication hakkında daha fazla bilgi için bkz. ASP.NET Authentication.

Şunlara uygulanır

Ayrıca bkz.