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属性值为 的 falsemanagerOnlyButton

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 或 将 ClientWindowsAuthenticationMembershipProviderstaticThread.CurrentPrincipal 属性设置为此类的实例。 将 ClientFormsAuthenticationMembershipProviderIdentity 属性初始化为 类的新实例 ClientFormsIdentity 。 将 ClientWindowsAuthenticationMembershipProvider 属性初始化 IdentityWindowsIdentity 方法返回的 staticWindowsIdentity.GetCurrent() 对象。

通常不会直接访问此类。 通常,你将调用 IsInRole 属性返回staticCurrentPrincipalIPrincipal 的 方法。 但是,可以将属性值ClientRolePrincipal强制转换为CurrentPrincipal引用以显式调用IsInRole方法,如示例部分所示。

构造函数

ClientRolePrincipal(IIdentity)

初始化 ClientRolePrincipal 类的新实例。

属性

Identity

获取与 ClientRolePrincipal 关联的安全标识符。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
IsInRole(String)

获取一个值,该值指示由 ClientRolePrincipal 表示的用户是否扮演指定的角色。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅