简体   繁体   English

未连接到域时如何获取Active Directory域组的列表

[英]How To Get List Of Active Directory Domain Groups When Not Connected To Domain

At my company we have a custom HMI. 在我公司,我们有一个自定义的HMI。 This HMI can login using the domain of the computer if the user would like to. 如果用户愿意,此HMI可以使用计算机的域登录。 Here's the scenario we're currently trying to solve 这是我们当前正在尝试解决的方案

  1. With user logged into Windows as a domain user, they login to the HMI using the same windows credentials - everything goes smoothly. 用户以域用户身份登录Windows后,他们使用相同的Windows凭据登录到HMI-一切顺利。

  2. Network connection goes down and the user restarts the computer. 网络连接断开,用户重新启动计算机。

  3. The user can login to Windows as before using the same domain logon (with the Windows cached user allowing this) 用户可以像以前一样使用相同的域登录登录Windows(Windows缓存用户允许这样做)

  4. The user tries to login to our HMI and they can't because I haven't figured out how to access these cached active directory users. 该用户尝试登录到我们的HMI,但不能,因为我还没有弄清楚如何访问这些缓存的Active Directory用户。

Here's the code I'm using right now to detect if the user is member of one of the groups allowed to login to the HMI. 这是我现在正在使用的代码,用于检测用户是否是允许登录到HMI的组之一。 These groups are stored in our SQL database, and are stored as Strings like SERVER\\BUILDER for example 这些组存储在我们的SQL数据库中,并以字符串形式存储,例如SERVER\\BUILDER

If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
            tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
            AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
            WinPrincipal = New WindowsPrincipal(tempWindowsIdentity)
End If

If WinPrincipal.IsInRole(user.Group) Then
            'Success - obviously there's more code after this
End If

The problem is that when the user is not connected to the domain I get an exception on the IsInRole method. 问题是,当用户未连接到域时, IsInRole方法会出现异常。 I took a look at the overloading in the method and tried using the SID equivalent of the user.Group instead of the literal String. 我查看了方法中的重载,并尝试使用等效于user.Group的SID而不是原义的String。 This seems to work, but now another problem comes up. 这似乎可行,但是现在又出现了另一个问题。 When the user is not on the network I can't translate the Group Name (string from SQL) into these SIDs. 当用户不在网络上时,我无法将组名(SQL中的字符串)转换为这些SID。

My current workaround is that when the user is connected to the domain, I save the SIDs and the groups into a lookup table (encrypted), then when the network is not connected, I take the group name from the database and literally lookup the SID string in the table. 我当前的解决方法是,当用户连接到域时,我将SID和组保存到查找表(加密)中,然后当网络未连接时,我从数据库中获取组名并从字面上查找SID表中的字符串。

The code I'm using to convert the group name to SID is the following 我用来将组名转换为SID的代码如下

'' Get all groups available to the user
        For i As Integer = 0 To WindowsIdentity.GetCurrent.Groups.Count - 1
            Dim ir As IdentityReference = WindowsIdentity.GetCurrent.Groups(i)
            sid(i) = New SecurityIdentifier(ir.Value)
            sidName(i) = CType(ir.Translate(GetType(NTAccount)), NTAccount).Value

            ' Write to file as csv
            writer.WriteLine(sidName(i) + "," + sid(i).ToString)
        Next

Then I lookup the group name like so 然后我像这样查找组名

If File.Exists(FILEPATH) = True Then
        Dim reader As New StreamReader(FILEPATH)
        While Not reader.EndOfStream
            Dim sTemp() As String = reader.ReadLine.Split(",")
            If groupName.Equals(sTemp(0)) Then
                Return New SecurityIdentifier(sTemp(1))
            End If
        End While
    End If

The reason I can't translate the group name to a SID when not on the network is because the IdentityReference.Translate throws an exception because it can't see the domain. 我不在网络上时无法将组名转换为SID的原因是因为IdentityReference.Translate抛出异常,因为它看不到域。

So, finally after all this explanation, here's what I'm looking for. 因此,在完成所有这些解释之后,这就是我想要的。 I'm thinking that there must be access somewhere for me to get the SID from a group name, because windows MUST be storing it somewhere, otherwise I wouldn't be able to login to windows when not connected to the network. 我在想,必须有某个地方可以让我从组名中获取SID,因为Windows必须将其存储在某个地方,否则当我未连接到网络时我将无法登录到Windows。

I've done a lot of searching and I haven't been able to figure out where these cached credentials are stored. 我已经进行了很多搜索,但无法弄清楚这些缓存的凭据存储在哪里。 Any help would be appreciated and if I haven't been clear enough let me know and I'll try to explain as best I can. 任何帮助将不胜感激,如果我不够清楚,请告诉我,我将尽力解释。

I know it is old, but it was actual for me. 我知道它很旧,但是对我来说是真实的。 As a familiar post I will link this: Determine User Active Directory Groups from Local Machine off Network 作为一个熟悉的帖子,我将链接此链接: 从网络外的本地计算机确定用户Active Directory组

The proposed solution was to store the SID in a local space which seems to be the best idea. 提出的解决方案是将SID存储在本地空间中,这似乎是最好的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM