1

我正在尝试匹配一个特定的组名,看看它是否存在于当前使用 Active Directory 角色登录的用户。如果当前用户的组名存在,我希望该组名显示在下拉列表中。示例:如果当前用户在 BIG Group 中,则在下拉列表中显示 BIG。

问题:我得到的只是 SID,我无法获得与组名匹配的任何内容,并且下拉列表中不会显示任何内容。

我还收到以下错误:

         Error: Object variable or WIth block variable not set.

我该如何解决??

这是我正在使用的代码:

Private Sub GetMarketingCompanies()

        ' code to populate marketing company drop down list based on the current logged in users active directory group that 
        ' corresponds to which marketing company they are in 

        Dim irc As IdentityReferenceCollection
        Dim ir As IdentityReference
        irc = WindowsIdentity.GetCurrent().Groups
        Dim strGroupName As String

        For Each ir In irc
            ' Dim mktGroup As IdentityReference = ir.Translate(GetType(NTAccount))
            MsgBox(mktGroup.Value)
            Debug.WriteLine(mktGroup.Value)
            strGroupName = mktGroup.Value.ToString
        Next 

        For Each UserGroup In WindowsIdentity.GetCurrent().Groups
            If mktGroup.Value = "BIG" Then
                Dim Company = ac1.Cast(Of MarketingCompany).Where(Function(ac) ac.MarketingCompanyShort = "BIG").FirstOrDefault
                If Company IsNot Nothing Then
                    marketingCo.Items.Add(String.Format("{0} | {1}", Company.MarketingCompanyShort, Company.MarketingCompanyName))
                End If
            End If
        Next

感谢您的关注!任何有用的答案都会被投票!

4

3 回答 3

2

我不确定您所指的角色是什么,但以下将列出当前用户组(本地和域):

For Each ir As IdentityReference In WindowsIdentity.GetCurrent.Groups
    Debug.WriteLine(CType(ir.Translate(GetType(NTAccount)), NTAccount).Value)
Next
于 2011-05-04T15:10:00.093 回答
0

我最终执行了以下操作来修复代码:

  • 删除在 WindowsIdentity.GetCurrent().Groups 中调用 UserGroup 的 For 循环
  • 将所有代码放在在 IdentityReferenceCollection 中调用 IdentityReference 的 For Each 循环下
  • 添加 mcisloaded 布尔变量以使 admin,而不是 admin if 语句有效
  • 禁用 MsgBox(mktGroup.Value) 因为这只是为了试错以查看返回的值

这是代码:

Private Sub GetMarketingCompanies()
    Try
        Dim ac1 As Array
        ac1 = proxy.GetMarketingCompanyNames("test", "test")

        ' code to populate marketing company drop down list based on the current logged in users active directory group that 
        ' corresponds to which marketing company they are in 

        Dim irc As IdentityReferenceCollection
        Dim ir As IdentityReference
        irc = WindowsIdentity.GetCurrent().Groups
        Dim strGroupName As String
        Dim mcisloaded As Boolean

        ' Translate the current user's active directory groups 

        For Each ir In irc
            Dim mktGroup As IdentityReference = ir.Translate(GetType(NTAccount))
            ' MsgBox(mktGroup.Value)
            Debug.WriteLine(mktGroup.Value)
            strGroupName = mktGroup.Value.ToString

            ' If the user is in the admin group, load all marketing companies   
            If mktGroup.Value = "ALG\ACOMP_USER_ADMIN" Then
                mcisloaded = True
                For Each item In ac1
                    marketingCo.Items.Add(String.Format("{0} | {1}", item.MarketingCompanyShort, item.MarketingCompanyName))
                Next
            End If

            'If the user is not in the admin group, load marketing companies individually
            If Not mktGroup.Value = "ALG\ACOMP_USER_ADMIN" Then
                mcisloaded = False

                If mcisloaded = False Then

                    If mktGroup.Value = "ALG\ACOMP_USER_BIG" Then
                        Dim Company = ac1.Cast(Of MarketingCompany).Where(Function(ac) ac.MarketingCompanyShort = "BIG").FirstOrDefault
                        If Company IsNot Nothing Then
                            marketingCo.Items.Add(String.Format("{0} | {1}", Company.MarketingCompanyShort, Company.MarketingCompanyName))
                        End If
                    End If

                    If mktGroup.Value = "ALG\ACOMP_USER_AMG" Then
                        Dim Company = ac1.Cast(Of MarketingCompany).Where(Function(ac) ac.MarketingCompanyShort = "AMG").FirstOrDefault
                        If Company IsNot Nothing Then
                            marketingCo.Items.Add(String.Format("{0} | {1}", Company.MarketingCompanyShort, Company.MarketingCompanyName))
                        End If
                    End If

                    ' ... Code for loading the rest of the marketing groups 

                End If
            End If

更新 6-7-11:这是一个更简洁的版本,通过使用字符串拆分器获取标识营销公司的最后 3 个字母,而不是每个营销公司的一系列 if 语句,循环遍历所有活动目录组名称:

Private Sub GetMarketingCompanies()
    Try
        Dim marketingCompanyNamesArray As Array
        marketingCompanyNamesArray = proxy.GetMarketingCompanyNames("test", "test")

        ' code to populate marketing company drop down list based on the current logged in users active directory group that 
        ' corresponds to which marketing company they are in 

        Dim identityReferenceCollection As IdentityReferenceCollection
        Dim identityReference As IdentityReference
        identityReferenceCollection = WindowsIdentity.GetCurrent().Groups
        Dim strGroupName As String
        Dim mcisloaded As Boolean

        ' Translate the current user's active directory groups 
        For Each identityReference In identityReferenceCollection
            Dim mktGroup As IdentityReference = identityReference.Translate(GetType(NTAccount))
            ' MsgBox(mktGroup.Value)
            ' Debug.WriteLine(mktGroup.Value) 
            strGroupName = mktGroup.Value.ToString

            ' Locally User group is ALG\ACOMP_USER_ADMIN , deployed ALGWEB\ACOMP_USER_ADMIN
            ' If the user is in the admin group, load all marketing companies   
            If mktGroup.Value = "ALG\ACOMP_USER_ADMIN" Then
                mcisloaded = True
                For Each item In marketingCompanyNamesArray
                    marketingCo.Items.Add(String.Format("{0} | {1}", item.MarketingCompanyShort, item.MarketingCompanyName))
                Next

            Else
                'If not admin user (mcisloaded = False) load each group individually if it appears in AD 
                ' For Each UserGroup In WindowsIdentity.GetCurrent().Groups that begins with ALG\ACOMP_USER, load marketing companies 

                Dim MarketingCompanyShortName As String = ""
                Dim mktGroupName As String = mktGroup.Value
                If mktGroupName.StartsWith("ALG\ACOMP_USER") Then
                    Dim marketingGroupNameParts() As String = Split(mktGroupName, "_")
                    'Load MarketingCompanyShortName from the end of marketingGroupNameParts - example: ACOMP_USER_BIG
                    MarketingCompanyShortName = marketingGroupNameParts(2)

                    'If MarketingCompanyShortName exists, load it into the dropdownlist 
                    Dim Company = marketingCompanyNamesArray.Cast(Of MarketingCompany).Where(Function(ac) ac.MarketingCompanyShort = MarketingCompanyShortName).FirstOrDefault
                    If Company IsNot Nothing Then
                        marketingCo.Items.Add(String.Format("{0} | {1}", Company.MarketingCompanyShort, Company.MarketingCompanyName))
                    End If

                End If
            End If 
于 2011-05-05T14:08:20.423 回答
0

作为对您的回答的回应-让我感到震惊的是,如果这是您想要执行的操作,则以下操作可能更有效且更易于阅读:

Dim p As WindowsPrincipal = New WindowsPrincipal(WindowsIdentity.GetCurrent()) 
If p.IsInRole("ALG\ACOMP_USER_ADMIN") Then 
    'load all groups 
ElseIf p.IsInRole("ALG\ACOMP_USER_BIG") Then 
    'load BIG groups 
ElseIf p.IsInRole("ALG\ACOMP_USER_AMG") Then 
    'load AMG groups 
    'etc
End If
于 2011-05-05T15:17:14.790 回答