【问题标题】:Handling AD Error When Not Connected未连接时处理 AD 错误
【发布时间】:2014-11-19 11:28:48
【问题描述】:

我正在使用下面的代码来获取 logge don 用户的登录名。这在连接到网络/域时工作正常。但是当离线时,我在第 3 行得到一个错误:

PrincipalServerDownException 未处理

无法联系到服务器。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
    currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current
    Dim DisplayName As String = currentADUser.GivenName & " " & currentADUser.Surname
    Label5.Text = DisplayName
    Label4.Text = GetUserProperties()
End Sub

我怎样才能检查它,并向用户返回一个有用的错误,例如“不在网络上”或“未连接到域”,然后退出?我已经尝试了以下方法,但这只是给出了同样的错误:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
    If System.DirectoryServices.AccountManagement.UserPrincipal.Current Is Nothing Then
        MsgBox("Network Error: No Connection to Domain")
        Application.Exit()
    Else ....
    End if
End Sub

【问题讨论】:

    标签: vb.net active-directory directoryservices


    【解决方案1】:

    使用 try/catch

        Try
            Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
            currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current
            Dim DisplayName As String = currentADUser.GivenName & " " & currentADUser.Surname
            Label5.Text = DisplayName
            Label4.Text = GetUserProperties()
        Catch ex As Exception
            MsgBox(...)
        End Try
    

    【讨论】:

    • 效果很好!谢谢!
    • 欢迎您。您可以使用 Catch ex As PrincipalServerDownException 使其更具体
    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 2014-05-08
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多