Domanda

I am trying to adapt code from VB to C# in a Windows form. I'm still trying to get a handle on the DFS idea in general, and how to manipulate it from a Windows form.

The VB uses the GetObject("LDAP://RootDSE") function to search the Active Directory for shares using a DirectorySearcher. I've adapted other functions that used that the same object to return a UserPrincipal from the user id, as well as checking if a Group already exists (using a GroupPrincipal). These usually go like this:

public static UserPrincipal GetUserPrincipal(string userId) {
    PrincipalContext context = new PrincipalContext(ContextType.Domain);
    UserPrincipal user = new UserPrincipal(context);
    user.Name = userId;
    PrincipalSearcher searcher = new PrincipalSearcher(user);
    return searcher.FindOne() as UserPrincipal;
}

However, I cannot find any documentation with the keywords I am using, but I am trying to get a list of directories that are DFS namespaces (I think).

Here is the (modified) code in VB:

Public Function GetDfsNamespaces() As List(Of String)
    Dim objRootDSE = GetObject("LDAP://RootDSE")
    Dim domain As String = objRootDSE.Get("DefaultNamingContext")
    Dim entry As New DirectoryEntry("LDAP://CN=DFs-Configuration,CN=System," & domain)
    Dim searcher As New DirectorySearcher(entry)
    searcher.PropertiesToLoad.Add("cn")
    searcher.Filter = "(objectClass=msDFS-NamespaceAnchor)"
    searcher.SearchScope = SearchScope.Subtree
    Dim results As SearchResultCollection = searcher.FindAll()
    Dim strResults As New List(Of String)
    For Each result In results
        strResults.Add(result.Properties("cn")(0))
    Next
    return strResults
End Function

I tried looking into the source of the UserPrincipal, GroupPrincipal and ComputerPrincipal but haven't been able to figure out how I'd extend the Principal object to get directories or something.

È stato utile?

Soluzione

First two lines should go like this:

        string domain;
        using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"))
        {
            domain = rootDSE.Properties["defaultNamingContext"].Value.ToString();
        }

The remaining code should be straightforward to convert.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top