2011-05-03 53 views
1

我想匹配特定的組名,並查看它是否存在當前登錄的用戶使用Active目錄角色。如果當前用戶的組名存在,我希望該組名顯示在下拉列表中。 例如:如果當前用戶在BIG組中,在下拉列表中顯示BIG。VB.NET - 如何查看當前用戶的組名是否與使用Active Directory角色和SID的指定組名匹配

問題:我所得到的只是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 

爲尋找謝謝! 任何有用的答案將被提高!

回答

0

我結束了做以下修復代碼:

  • 刪除For循環調用用戶組在WindowsIdentity.GetCurrent()。組
  • 把所有的代碼下的每個循環調用的IdentityReference在IdentityReferenceCollection
  • 加入mcisloaded布爾變量來進行管理,而不是管理的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 

更新11年6月7日:這是使用字符串分離器獲得最後3個字母標識營銷公司通過所有的Active Directory組名騎自行車的清潔版本,而不是一個系列的如果每個營銷公司報表爲:

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 
2

我不知道你指的是通過什麼樣的角色,但下面會列出當前用戶組(本地和域):

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

感謝您的答覆。那麼,一個組在web.config中被稱爲角色。其餘的呢? – 2011-05-05 13:22:41

+0

@Brian McCarthy - 剩下的? (也許你需要在提到「使用Active Directory角色」時稍微修改你的問題) – 2011-05-05 13:42:21

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 
相關問題