VB.NET - 如何使用 Active Directory 角色和 SID 查看当前用户的组名是否与指定的组名匹配

发布于 2024-11-04 16:40:27 字数 1504 浏览 2 评论 0原文

我正在尝试匹配特定的组名称,并查看当前使用 Active Directory 角色登录的用户是否存在该组名称。如果当前用户存在组名称,我希望该组名称显示在下拉列表中。 示例:如果当前用户在 BIG 组中,则在下拉列表中显示 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

感谢您的查看! 任何有用的答案都将被投票!

I'm trying to match up a specific group name and see if it exists for the currently logged in user using Active Directory roles. If the Group Name exists for the Current User, I want that group name to be displayed in a drop down list.
Example: If current user is in BIG Group, display BIG in drop down list.

Problem: All I am getting is SIDs and I'm not able to get anything to match up to the group name and nothing will show up in the drop down list.

I also get the following Error:

         Error: Object variable or WIth block variable not set.

How do I fix this??

here is the code I am using:

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

Thanks for looking!
Any helpful answers will be up-voted!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光匆匆的小流年 2024-11-11 16:40:27

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

For Each ir As IdentityReference In WindowsIdentity.GetCurrent.Groups
    Debug.WriteLine(CType(ir.Translate(GetType(NTAccount)), NTAccount).Value)
Next

I'm not sure what you are referring to by roles but the following will list the current users groups (both local and domain):

For Each ir As IdentityReference In WindowsIdentity.GetCurrent.Groups
    Debug.WriteLine(CType(ir.Translate(GetType(NTAccount)), NTAccount).Value)
Next
心头的小情儿 2024-11-11 16:40:27

回应你的回答 - 令我震惊的是,如果这就是你想要做的,那么以下可能更有效且更易于阅读:

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

In response to your answer - Strikes me that if this is what you want to do the following is probably more efficient and easier to read:

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
千紇 2024-11-11 16:40:27

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

  • 删除在 WindowsIdentity.GetCurrent().Groups 中调用 UserGroup 的 For 循环
  • ,将所有代码放在在 IdentityReferenceCollection 中调用 IdentityReference 的 For Each 循环下,
  • 添加 mcisloaded 布尔变量以使管理员,不是管理员如果语句工作
  • 禁用 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 

I ended up doing the following to fix the code:

  • deleting the the For loop that calls UserGroup In WindowsIdentity.GetCurrent().Groups
  • putting all the code under the For Each Loop that calls IdentityReference In IdentityReferenceCollection
  • adding mcisloaded boolean variable to make the admin, not admin if statements work
  • disabling MsgBox(mktGroup.Value) as this was just for trial and error to see what values were getting returned

Here's the code:

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

Update 6-7-11: Here's a cleaner version of cycling through all the active directory group names by using a string splitter to get the last 3 letters that identifies the marketing company, instead of a series of if statements for each marketing company:

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文