2008-11-24 6 views

Répondre

4

Vous pouvez le faire avec l'espace de noms System.DirectoryServices.

Dim entry As DirectoryServices.DirectoryEntry 
Dim mySearcher As System.DirectoryServices.DirectorySearcher 
Dim result As System.DirectoryServices.SearchResult 
Dim myEntry As DirectoryEntry 
Dim domainName As String 
Dim userId As String 
Dim objectGuid As Guid 

'Split the username into domain and userid parts 
domainName = Page.User.Identity.Name.Substring(0, Page.User.Identity.Name.IndexOf("\")) 
userId = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\") + 1) 

'Start at the top level domain 
entry = New DirectoryEntry(domainName) 

mySearcher = New DirectorySearcher(entry) 

'Build a filter for just the user 
mySearcher.Filter = ("(&(anr=" & userId & ")(objectClass=user))") 

'Get the search result ... 
result = mySearcher.FindOne 

'... and then get the AD entry that goes with it 
myEntry = result.GetDirectoryEntry 

'The Guid property is the objectGuid 
objectGuid = myEntry.Guid 

Il pourrait y avoir une meilleure façon de le faire, mais cela fonctionne!

+0

Il semble à peu près correct, je vais essayer demain. Merci. –

+0

Merci. Afin d'obtenir l'objet correct, j'ai utilisé ce code à la place: objectGuid = System.Guid.Parse (myEntry.NativeGuid) – geekinit

2

Vous devez utiliser la propriété NativeGuid. Code C#:

string login = HttpContext.Current.User.Identity.Name; 
string domain = login.Substring(0, login.IndexOf('\\')); 
string userName = login.Substring(login.IndexOf('\\') + 1); 
DirectoryEntry domainEntry = new DirectoryEntry("LDAP://" + domain); 
DirectorySearcher searcher = new DirectorySearcher(domainEntry); 
searcher.Filter = string.Format(
    "(&(objectCategory=person)(objectClass=user)(sAMAccountName={0}))", 
    userName); 
SearchResult searchResult = searcher.FindOne(); 
DirectoryEntry entry = searchResult.GetDirectoryEntry(); 
Guid objectGuid = new Guid(entry.NativeGuid); 
11

Les solutions suggérées sont plutôt coûteuses. Plutôt que de rechercher par domaine et nom d'utilisateur, une meilleure solution consiste à utiliser le SID pour rechercher le compte:

// using System.Security.Principal; 
IPrincipal userPrincipal = HttpContext.Current.User; 
WindowsIdentity windowsId = userPrincipal.Identity as WindowsIdentity; 
if (windowsId != null) 
{ 
    SecurityIdentifier sid = windowsId.User; 

    using(DirectoryEntry userDe = new DirectoryEntry("LDAP://<SID=" + sid.Value + ">")) 
    { 
     Guid objectGuid = new Guid(userDe.NativeGuid); 
    } 
} 
+0

Que faire si windowsId est null? – dpp

+0

Ensuite, vous avez probablement affaire à un utilisateur anonyme ou à quelqu'un qui ne fait pas confiance au site Web pour autoriser l'authentification Kerberos. Vous pourriez travailler avec les administrateurs de votre entreprise pour mettre en place une politique de groupe pour faire confiance au site * interne *. Ou vous pouvez fournir un formulaire de connexion https ou une autre méthode d'authentification. – Felan

Questions connexes