ClientRolePrincipal 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表用戶端應用程式服務的安全性資訊,包括角色資訊。
public ref class ClientRolePrincipal : System::Security::Principal::IPrincipal
public class ClientRolePrincipal : System.Security.Principal.IPrincipal
type ClientRolePrincipal = class
interface IPrincipal
Public Class ClientRolePrincipal
Implements IPrincipal
- 繼承
-
ClientRolePrincipal
- 實作
範例
下列範例程式碼示範如何在使用者處於「管理員」角色時,使用此類別來顯示按鈕。 此範例需要 Button 具有 初始 Visible 屬性值為 的 false
具名 managerOnlyButton
。
private void DisplayButtonForManagerRole()
{
try
{
ClientRolePrincipal rolePrincipal =
System.Threading.Thread.CurrentPrincipal
as ClientRolePrincipal;
if (rolePrincipal != null && rolePrincipal.IsInRole("manager"))
{
managerOnlyButton.Visible = true;
}
}
catch (System.Net.WebException)
{
MessageBox.Show("Unable to access the roles service.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Private Sub DisplayButtonForManagerRole()
Try
Dim rolePrincipal As ClientRolePrincipal = TryCast( _
System.Threading.Thread.CurrentPrincipal, ClientRolePrincipal)
If rolePrincipal IsNot Nothing And _
rolePrincipal.IsInRole("manager") Then
managerOnlyButton.Visible = True
End If
Catch ex As System.Net.WebException
MessageBox.Show("Unable to access the role service.", _
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End Sub
備註
當您在用戶端應用程式服務中驗證使用者時, ClientFormsAuthenticationMembershipProvider 或 會將 ClientWindowsAuthenticationMembershipProvider 屬性設定 static
Thread.CurrentPrincipal 為這個類別的實例。 會將 ClientFormsAuthenticationMembershipProvider 屬性初始化 Identity 為 類別的新實例 ClientFormsIdentity 。 會將 ClientWindowsAuthenticationMembershipProvider 屬性初始化 Identity 為 WindowsIdentity 方法所 static
WindowsIdentity.GetCurrent() 傳回的物件。
您通常不會直接存取此類別。 一般而言,您將會呼叫 IsInRole 屬性所 static
CurrentPrincipal 傳回的 IPrincipal 方法。 不過,您可以將屬性值 ClientRolePrincipal 轉換成 CurrentPrincipal 參考,以明確呼叫 IsInRole 方法,如範例一節所示。
建構函式
ClientRolePrincipal(IIdentity) |
初始化 ClientRolePrincipal 類別的新執行個體。 |
屬性
Identity |
取得與 ClientRolePrincipal 相關聯的安全性識別。 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
IsInRole(String) |
取得值,指出 ClientRolePrincipal 表示的使用者是否在指定的角色中。 |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應