Roles.GetRolesForUser Yöntem

Tanım

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

Aşırı Yüklemeler

GetRolesForUser()

Şu anda oturum açmış olan kullanıcının içinde bulunduğu rollerin listesini alır.

GetRolesForUser(String)

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

GetRolesForUser()

Şu anda oturum açmış olan kullanıcının içinde bulunduğu rollerin listesini alır.

public:
 static cli::array <System::String ^> ^ GetRolesForUser();
public static string[] GetRolesForUser ();
static member GetRolesForUser : unit -> string[]
Public Shared Function GetRolesForUser () As String()

Döndürülenler

String[]

Şu anda oturum açmış olan kullanıcının içinde bulunduğu tüm rollerin adlarını içeren bir dize dizisi.

Özel durumlar

Şu anda oturum açmış bir kullanıcı yok.

Rol yönetimi etkinleştirilmedi.

Örnekler

Aşağıdaki kod örneği, belirtilen kullanıcının rol listesini almak ve döndürülen rolleri bir System.Web.UI.WebControls.GridView denetime bağlamak için yöntemini kullanırGetRolesForUser. Rol yönetimini etkinleştiren bir Web.config dosyası örneği için bkz 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;

public void Page_Load()
{
  if (!IsPostBack)
  {
    // Bind roles to GridView.

    try
    {
      rolesArray = Roles.GetRolesForUser();
    }
    catch (HttpException e)
    {
      Msg.Text = "There is no current logged on user. Role information cannot be retrieved.";
      return;
    }

    UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
    UserRolesGrid.DataSource = rolesArray;
    UserRolesGrid.DataBind();
  }
}

</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()
  If Not IsPostBack Then
    ' Bind roles to GridView.

    Try
      rolesArray = Roles.GetRolesForUser()
    Catch e As HttpException
      Msg.Text = "There is no current logged on user. Role information cannot be retrieved."
      Return
    End Try

    UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
    UserRolesGrid.DataSource = rolesArray
    UserRolesGrid.DataBind()
  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: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

yöntemi, GetRolesForUser şu anda oturum açmış olan kullanıcının bulunduğu rolleri veri kaynağından almak için varsayılan rol sağlayıcısının yöntemini çağırır RoleProvider.GetRolesForUser . Şu anda oturum açmış olan kullanıcı, geçerli öğesinin HttpContext.User özelliği veya HTTP olmayan barındırma ortamları için tarafından Thread.CurrentPrincipal tanımlanır.System.Web.HttpContext Hiçbir kullanıcı oturum açmadıysa, bir özel durum oluşturulur. Yalnızca özelliğinde ApplicationName belirtilen uygulamanın rolleri alınır.

ise CacheRolesInCookie true, yöntemin GetRolesForUser sonuçları belirtilen rol sağlayıcısı yerine rol önbelleğinden döndürülebilir.

Ayrıca bkz.

Şunlara uygulanır

GetRolesForUser(String)

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

public:
 static cli::array <System::String ^> ^ GetRolesForUser(System::String ^ username);
public static string[] GetRolesForUser (string username);
static member GetRolesForUser : string -> string[]
Public Shared Function GetRolesForUser (username As String) As String()

Parametreler

username
String

Rollerin listesini döndürecek kullanıcı.

Döndürülenler

String[]

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

Özel durumlar

username, null değeridir.

username virgül (,) içerir.

Rol yönetimi etkinleştirilmedi.

Örnekler

Aşağıdaki kod örneği, belirtilen kullanıcının rol listesini almak ve döndürülen rolleri bir System.Web.UI.WebControls.GridView denetime bağlamak için yöntemini kullanırGetRolesForUser. Rol yönetimini etkinleştiren bir Web.config dosyası örneği için bkz 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;

public void Page_Load()
{
  if (!IsPostBack)
  {
    // Bind roles to GridView.

    try
    {
      rolesArray = Roles.GetRolesForUser();
    }
    catch (HttpException e)
    {
      Msg.Text = "There is no current logged on user. Role information cannot be retrieved.";
      return;
    }

    UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
    UserRolesGrid.DataSource = rolesArray;
    UserRolesGrid.DataBind();
  }
}

</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()
  If Not IsPostBack Then
    ' Bind roles to GridView.

    Try
      rolesArray = Roles.GetRolesForUser()
    Catch e As HttpException
      Msg.Text = "There is no current logged on user. Role information cannot be retrieved."
      Return
    End Try

    UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
    UserRolesGrid.DataSource = rolesArray
    UserRolesGrid.DataBind()
  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: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

yöntemi, GetRolesForUser kullanıcının içinde olduğu rolleri veri kaynağından almak için varsayılan rol sağlayıcısının yöntemini çağırır RoleProvider.GetRolesForUser . Yalnızca özelliğinde ApplicationName belirtilen uygulamanın rolleri alınır.

Geçerli oturum açmış kullanıcıya eşitse username ve CacheRolesInCookie ise true, yönteminin GetRolesForUser sonuçları belirtilen Provideryerine rol önbelleğinden döndürülebilir.

Ayrıca bkz.

Şunlara uygulanır