共用方式為


ClientRolePrincipal 類別

定義

代表用戶端應用程式服務的安全性資訊,包括角色資訊。

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 屬性設定 staticThread.CurrentPrincipal 為這個類別的實例。 會將 ClientFormsAuthenticationMembershipProvider 屬性初始化 Identity 為 類別的新實例 ClientFormsIdentity 。 會將 ClientWindowsAuthenticationMembershipProvider 屬性初始化 IdentityWindowsIdentity 方法所 staticWindowsIdentity.GetCurrent() 傳回的物件。

您通常不會直接存取此類別。 一般而言,您將會呼叫 IsInRole 屬性所staticCurrentPrincipal傳回的 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)

適用於

另請參閱