Share via


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

請參閱

工作

HOW TO:判斷使用者的網域

逐步解說:實作自訂驗證和授權

概念

存取使用者資料

參考

My.User.Name 屬性