VB.NET - Come vedere se il nome del gruppo dell'utente corrente corrisponde a un nome di gruppo specificato usando ruoli Active Directory e SID

StackOverflow https://stackoverflow.com/questions/5872761

Domanda

Sto cercando di abbinare un nome di gruppo specifico e vedere se esiste per l'utente attualmente registrato utilizzando i ruoli di Active Directory. Se il nome del gruppo esiste per l'utente corrente, voglio che quel nome del gruppo venga visualizzato in un elenco di discesa. Esempio: se l'utente attuale è in grande gruppo, visualizza in grande elenco a discesa.

Problema: tutto ciò che sto ottenendo è SIDS e non sono in grado di ottenere nulla da abbinare al nome del gruppo e nulla verrà visualizzato nella lista a discesa.

Ricevo anche il seguente errore:

         Error: Object variable or WIth block variable not set.

Come lo risolvo ??

Ecco il codice che sto usando:

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

Grazie per aver guardato! Eventuali risposte utili saranno aggiornate!

È stato utile?

Soluzione 3

Ho finito per fare quanto segue per correggere il codice:

  • Eliminazione del loop che chiama User Group in WindowsIdentity.getCurrent ().
  • Mettere tutto il codice sotto il ciclo per ogni ciclo che chiama IdentityReference in IdentityreferenceCollection
  • Aggiungendo la variabile booleana McISloadd per rendere l'amministratore, non l'amministratore se funziona le dichiarazioni
  • disabilitare msgbox (mktgroup.value) in quanto questo era solo per prove ed errori per vedere quali valori venivano restituiti

Ecco il codice:

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

AGGIORNAMENTO 6-7-11: ecco una versione più pulita del ciclismo attraverso tutti i nomi dei gruppi di Active Directory utilizzando uno splitter String per ottenere le ultime 3 lettere che identificano la società di marketing, anziché una serie di dichiarazioni IF per ciascuna società di marketing:

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 

Altri suggerimenti

Non sono sicuro a cosa ti riferisci per ruoli, ma quanto segue elencherà i gruppi di utenti attuali (sia locali che di dominio):

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

In risposta alla tua risposta - mi colpisce che se questo è quello che vuoi fare è probabilmente più efficiente e più facile da leggere:

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top