【问题标题】:IsInRole - VB Form LoadIsInRole - VB 表单加载
【发布时间】:2015-04-23 14:29:43
【问题描述】:

我用 Visual Basic 构建了一个带有登录屏幕和表单的应用程序。登录屏幕通过 Active Directory 进行身份验证。用户认证后,表单加载。在表单加载时,我想检查经过身份验证的用户是否在四个特定的 Active Directory 安全组之一中。根据经过身份验证的用户所在的组将取决于启用表单上的哪些按钮。我已经获得了活动目录用户身份验证,可用于登录程序并加载表单,但用于验证用户所在组的特定代码不起作用。下面是我的表单加载代码。

Private Sub form_main_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    button_main_pimam.Enabled = False
    button_main_pimpm.Enabled = False
    button_main_eim.Enabled = False
    button_main_achmanager.Enabled = False
    button_main_mobiliti.Enabled = False
    button_main_checkfree.Enabled = False
    button_main_rcm.Enabled = False
    button_main_mis.Enabled = False
    button_main_colson.Enabled = False

    If My.User.IsInRole("domain.local\Fiserv Processing - Electronic Banking") Then
        button_main_achmanager.Enabled = True
        button_main_pimam.Enabled = True
        button_main_pimpm.Enabled = True
        button_main_eim.Enabled = True
        button_main_colson.Enabled = True
        button_main_colson.Enabled = True
    ElseIf My.User.IsInRole("domain.local\Fiserv Processing - Operations") Then
        button_main_achmanager.Enabled = True
        button_main_mobiliti.Enabled = True
        button_main_checkfree.Enabled = True
        button_main_rcm.Enabled = True
        button_main_colson.Enabled = True
    ElseIf My.User.IsInRole("domain.local\Fiserv Processing - Loan Operations") Then
        button_main_pimam.Enabled = True
        button_main_pimpm.Enabled = True
        button_main_eim.Enabled = True
        button_main_achmanager.Enabled = True
        button_main_mobiliti.Enabled = True
        button_main_checkfree.Enabled = True
        button_main_rcm.Enabled = True
        button_main_mis.Enabled = True
    ElseIf My.User.IsInRole("domain.local\Fiserv Processing - MIS") Then
        button_main_pimam.Enabled = True
        button_main_pimpm.Enabled = True
        button_main_eim.Enabled = True
        button_main_achmanager.Enabled = True
        button_main_mobiliti.Enabled = True
        button_main_checkfree.Enabled = True
        button_main_rcm.Enabled = True
        button_main_mis.Enabled = True
        button_main_colson.Enabled = True
    End If
End Sub

无论经过身份验证的用户属于哪个组,所有按钮都可以使用。我做错了什么?

【问题讨论】:

  • 使用调试器单步执行此代码,注意终止此代码但不生成调试器诊断的异常。 Fwiw,这段代码属于构造函数。
  • 调试器没有抛出任何错误或异常。代码不会终止,只是无法正常工作。
  • 你将不得不找到一个网站,只要你不告诉我们确切的如何你可以告诉我们它不起作用,心理医生会免费提供建议.突然间,将 button_main_colson.Enabled 设置为 True 两次是一种代码味道。
  • 我经过身份验证的用户在“操作”AD 组中。根据上面的代码,一旦我登录程序,我应该只启用 9 个按钮中的 5 个。但是当我登录时,我启用了所有 9 个按钮。
  • 也许我应该重新表述我的问题。我应该如何编写代码以启用表单加载时的某些按钮并根据经过身份验证的用户所属的 AD 安全组禁用其他按钮。

标签: vb.net active-directory isinrole


【解决方案1】:

试试这个方法。在您的情况下,我会在用户进行身份验证时缓存用户所属的组数组,然后在您的应用程序中随时检查。

   Function IsInGroup(UserName As String, groupName As String) As Boolean
      Dim vUsuario As New NTAccount(UserName)
      Dim sid As SecurityIdentifier = vUsuario.Translate(GetType(SecurityIdentifier))
      Using vRootDSE As New DirectoryEntry("LDAP://rootDSE")
         Using vSearcher As New DirectorySearcher(New DirectoryEntry("LDAP://" + CStr(vRootDSE.Properties("defaultNamingContext")(0))), "(objectSID=" & sid.ToString() & ")", New String() {"memberOf"}, SearchScope.Subtree)
            Dim src As SearchResultCollection = vSearcher.FindAll()

            Dim memberOf As ResultPropertyValueCollection = src(0).Properties("memberOf")
            For i As Integer = 0 To memberOf.Count - 1
               'Debug.Print(memberOf(i).ToString())

               ' I don't really like this approach, but it's quick to write ;)
               If memberOf(i).ToString().Contains("=" & groupName & ",") Then
                  Return True
               End If
            Next

         End Using

      End Using

      Return False
   End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多