WindowsTokenRoleProvider.GetRolesForUser(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取用户在其中Windows组的列表。
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()
参数
- username
- String
要在 DOMAIN\username 中返回Windows组列表的用户。
返回
一个字符串数组,其中包含指定用户所在的所有Windows组的名称。
例外
当前正在执行的用户未附加WindowsIdentity经过身份验证User。 对于非 HTTP 方案,当前正在执行的用户未附加WindowsIdentity经过身份验证CurrentPrincipal。
-或-
username与当前Name类型的值不匹配WindowsIdentity。
-或-
检索用户的 Windows 组信息时出错。
username 是 null。
信任级别小于 Low。
示例
下面的代码示例使用 GetRolesForUser 该方法检索指定用户的角色列表,并将角色 GridView 列表绑定到控件。 有关启用角色管理的 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>
注解
此方法由 Roles 类调用,以便从Windows操作系统中检索指定用户位于的Windows组的列表。
GetRolesForUser只能为当前登录的用户调用该方法,由LOGON_USER服务器变量标识。 如果参数中 username 提供的值不是当前登录用户的名称,则会引发一个 System.Configuration.Provider.ProviderException 。
有关 ASP.NET 和Windows 身份验证的详细信息,请参阅 ASP.NET 身份验证。