以前工作代码的新问题,用于查找用户所属的所有 AD 组(部分或所有身份引用无法翻译)
Posted
技术标签:
【中文标题】以前工作代码的新问题,用于查找用户所属的所有 AD 组(部分或所有身份引用无法翻译)【英文标题】:New Issue for previously working code to find all AD groups a user is a member of (Some or all identity references could not be translated) 【发布时间】:2021-06-01 12:33:19 【问题描述】:我的代码可以返回 Windows 窗体应用程序的当前用户所属的所有 AD 组的列表。这用于确定访问权限。它已经工作了一段时间,但突然间它不起作用并返回错误“无法翻译部分或全部身份引用”。请注意,当我在虚拟机中调试代码时不会发生此错误,但仅在我发布它并尝试从我们公司的共享驱动器访问它时发生。这可能是由于某些网络安全更改,并且与域权限或类似的东西有关,但我的 IT 部门在这方面没有帮助。
错误在下面一行:
Dim group As String = id_ref.Translate(GetType(NTAccount)).Value
整个函数如下:
Shared Function GetGroupNames() As List(Of String)
'Returns a list of all groups the current user is a member of. Called from CheckAccess.
Dim groups As New List(Of String)
For Each id_ref As IdentityReference In WindowsIdentity.GetCurrent().Groups
Dim group As String = id_ref.Translate(GetType(NTAccount)).Value
If group.Contains("\") Then
'Remove unneeded domain info.
group = group.Split("\")(1).ToLower
End If
groups.Add(group)
Next
Return groups
End Function
【问题讨论】:
尝试以下操作:Dim group As String = id_ref?.Translate(GetType(System.Security.Principal.NTAccount)).Value
和 If Not String.IsNullOrEmpty(group) AndAlso group.Contains("\") Then
。有关详细信息,请参阅以下文章:docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/…
谢谢,这解决了我的问题,我的功能现在可以完美运行了!另外,那个.? null 条件运算符是我以前不知道的,将来可能会有用。太棒了!
如果您希望我将此标记为答案,只需将其发布为答案,我会这样做。
【参考方案1】:
在调用Translate
之前确保id_ref
不为空。
选项 1:
Dim group As String = id_ref?.Translate(GetType(System.Security.Principal.NTAccount)).Value
获取组名
Shared Function GetGroupNames() As List(Of String)
'Returns a list of all groups the current user is a member of. Called from CheckAccess.
Dim groups As New List(Of String)
For Each id_ref As System.Security.Principal.IdentityReference In System.Security.Principal.WindowsIdentity.GetCurrent().Groups
Dim group As String = id_ref?.Translate(GetType(System.Security.Principal.NTAccount)).Value
If Not String.IsNullOrEmpty(group) Then
If group.Contains("\") Then
'Remove unneeded domain info.
group = group.Split("\")(1).ToLower
End If
groups.Add(group)
End If
Next
Return groups
End Function
注意:请参阅?. and ?() null-conditional operators (Visual Basic) 了解更多信息。
选项 2:
获取组名
Shared Function GetGroupNames() As List(Of String)
'Returns a list of all groups the current user is a member of. Called from CheckAccess.
Dim groups As New List(Of String)
For Each id_ref As System.Security.Principal.IdentityReference In System.Security.Principal.WindowsIdentity.GetCurrent().Groups
If id_ref IsNot Nothing Then
Dim group As String = id_ref.Translate(GetType(System.Security.Principal.NTAccount)).Value
If Not String.IsNullOrEmpty(group) Then
If group.Contains("\") Then
'Remove unneeded domain info.
group = group.Split("\")(1).ToLower
End If
groups.Add(group)
End If
End If
Next
Return groups
End Function
【讨论】:
以上是关于以前工作代码的新问题,用于查找用户所属的所有 AD 组(部分或所有身份引用无法翻译)的主要内容,如果未能解决你的问题,请参考以下文章
通过powershell查询OU中被禁用的AD账号,并删除他们的所属组