User.CurrentPrincipal 屬性

定義

取得或設定目前的原則(用於基於角色的安全性)。

public:
 property System::Security::Principal::IPrincipal ^ CurrentPrincipal { System::Security::Principal::IPrincipal ^ get(); void set(System::Security::Principal::IPrincipal ^ value); };
public System.Security.Principal.IPrincipal CurrentPrincipal { get; set; }
member this.CurrentPrincipal : System.Security.Principal.IPrincipal with get, set
Public Property CurrentPrincipal As IPrincipal

屬性值

一個 IPrincipal 代表安全情境的值。

例外狀況

來電者沒有設定委託人的權限。

範例

此範例檢查應用程式是否使用Windows或自訂認證,並利用這些資訊解析 My.User.Name 屬性。

Function GetUserName() As String
    If TypeOf My.User.CurrentPrincipal Is 
      Security.Principal.WindowsPrincipal Then
        ' The application is using Windows authentication.
        ' The name format is DOMAIN\USERNAME.
        Dim parts() As String = Split(My.User.Name, "\")
        Dim username As String = parts(1)
        Return username
    Else
        ' The application is using custom authentication.
        Return My.User.Name
    End If
End Function

備註

你可以將該屬性設定 My.User.CurrentPrincipal 為實 IPrincipal 作介面以啟用自訂認證的物件。

在大多數專案類型中,這個屬性會取得並設定執行緒目前的主體。 在 ASP.NET 應用程式中,此屬性會取得並設定當前 HTTP 請求使用者身份的安全資訊。

這是進階成員;除非你點選 「全部 」標籤,否則它不會在 IntelliSense 中顯示。

依專案類型提供可用性

專案類型 Available
Windows 應用程式 Yes
類別庫 Yes
主控台應用程式 Yes
Windows 控件庫 Yes
Web 控制庫 Yes
Windows 服務 Yes
網站 Yes

適用於

另請參閱