VB.NET - 如何使用 Active Directory 角色和 SID 查看当前用户的组名是否与指定的组名匹配
我正在尝试匹配特定的组名称,并查看当前使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定您所指的角色是什么,但以下将列出当前用户组(本地和域):
I'm not sure what you are referring to by roles but the following will list the current users groups (both local and domain):
回应你的回答 - 令我震惊的是,如果这就是你想要做的,那么以下可能更有效且更易于阅读:
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:
我最终执行了以下操作来修复代码:
这是代码:
更新 6-7-11:这是循环遍历所有活动目录组的更干净的版本通过使用字符串拆分器来获取标识营销公司的最后 3 个字母来命名,而不是针对每个营销公司使用一系列 if 语句:
I ended up doing the following to fix the code:
Here's the code:
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: