更新:2007 年 11 月
取得或設定目前的主體 (適用於以角色為基礎的安全性)。
' Usage Dim value As System.Security.Principal.IPrincipal = My.User.CurrentPrincipal ' Declaration Public Property CurrentPrincipal As System.Security.Principal.IPrincipal
傳回值
IPrincipal 值,代表安全性內容。
例外狀況
下列條件可能造成例外狀況:
- 呼叫端沒有設定主體所需的使用權限 (SecurityException)。
備註
您可以將 My.User.CurrentPrincipal 屬性設為實作 IPrincipal 介面的物件,即可啟用自訂驗證 (Authentication)。
在大部分的專案類型中,這個屬性會取得和設定執行緒目前的主體。在 ASP.NET 應用程式中,這個屬性會取得和設定目前 HTTP 要求之使用者身分的安全性資訊。
這是進階的成員,除非您按一下 [全部] 索引標籤,否則不會顯示在 IntelliSense 中。
工作
若要 |
請參閱 |
|---|---|
取得使用者的登入名稱 |
|
取得使用者的網域名稱 (如果應用程式使用 Windows 驗證) |
|
實作自訂驗證 |
範例
這個範例會檢查應用程式是使用 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
需求
命名空間:Microsoft.VisualBasic.ApplicationServices
組件:Visual Basic 執行階段程式庫 (在 Microsoft.VisualBasic.dll 中)
依專案類型的可用性
專案類型 |
是否可用 |
|---|---|
Windows 應用程式 |
是 |
類別庫 |
是 |
主控台應用程式 |
是 |
Windows 控制項程式庫 |
是 |
Web 控制項程式庫 |
是 |
Windows 服務 |
是 |
網站 |
是 |
使用權限
下列使用權限可能為必要:
使用權限 |
描述 |
|---|---|
描述套用至程式碼的一組安全性權限。關聯的列舉型別:ControlPrincipal。 |