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
使用者需回傳 Windows 群組清單,格式為 DOMAIN\username。
傳回
一個包含指定使用者所屬所有 Windows 群組名稱的字串陣列。
例外狀況
目前執行中的使用者沒有綁定 的認證WindowsIdentity。User 對於非 HTTP 情境,目前執行中的使用者沒有綁定 WindowsIdentity的認證CurrentPrincipal。
-或-
username 與 的 不符 NameWindowsIdentity。
-或-
在取得使用者 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 server 變數識別。 如果參數中提供的 username 值不是目前登入使用者的名稱,則會拋出 a System.Configuration.Provider.ProviderException 。
欲了解更多 ASP.NET 與Windows 驗證資訊,請參見 ASP.NET 認證。