HOW TO:判斷使用者的登入名稱
更新:2007 年 11 月
您可以使用 My.User 物件,取得目前使用者的資訊。這個範例會顯示如何使用 My.User.Name 屬性 (Property),取得使用者的登入名稱。
根據預設,應用程式會使用 Windows 驗證 (Authentication),My.User 就會傳回啟動應用程式之使用者的 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