Esiste una classe helper per Active Directory disponibile da qualche parte? Sto solo controllando prima di re-inventare la ruota.Classe helper Active Directory
ho bisogno di
Convalida un utente in AD.
Ottieni i suoi ruoli membri.
Grazie
Esiste una classe helper per Active Directory disponibile da qualche parte? Sto solo controllando prima di re-inventare la ruota.Classe helper Active Directory
ho bisogno di
Convalida un utente in AD.
Ottieni i suoi ruoli membri.
Grazie
In .NET 3.5, si vuole guardare in System.DirectoryServices.AccountManagement. Per prima cosa, le versioni System.DirectoryServices hanno quello di cui hai bisogno, ma è un po 'più di lavoro.
using (var context = new PrincipalContext(ContextType.Domain))
{
var valid = context.ValidateCredentials(username, password);
using (var user = UserPrincipal.FindByIdentity(context,
IdentityType.SamAccountName,
username))
{
var groups = user.GetAuthorizationGroups();
}
}
System.DirectoryServices.ActiveDirectory namespace
http://msdn.microsoft.com/en-us/library/system.directoryservices.activedirectory.aspx
Questo è veramente per l'interazione con e gestire gli aspetti amministrativi della ActiveDirectory - schema, server, siti, foreste, ecc L'utente i componenti correlati sono in uno spazio dei nomi diverso. – tvanfosson
Grazie per la correzione, debitamente annotato –
Ecco qualche esempio di codice che ho utilizzato:
using System.DirectoryServices;
public static string GetProperty(SearchResult searchResult,
string PropertyName)
{
if (searchResult.Properties.Contains(PropertyName))
return searchResult.Properties[PropertyName][0].ToString();
else
return string.Empty;
}
public MyCustomADRecord Login(string UserName, string Password)
{
string adPath = "LDAP://www.YourCompany.com/DC=YourCompany,DC=Com";
DirectorySearcher mySearcher;
SearchResult resEnt;
DirectoryEntry de = new DirectoryEntry(adPath, UserName, Password,
AuthenticationTypes.Secure);
mySearcher = new DirectorySearcher(de);
string adFilter = "(sAMAccountName=" + UserName + ")";
mySearcher.Filter = adFilter;
resEnt = mySearcher.FindOne();
return new MyCustomADRecord()
{
UserName = GetProperty(resEnt, "sAMAccountName"),
GUID = resEnt.GetDirectoryEntry().NativeGuid.ToString(),
DisplayName = GetProperty(resEnt, "displayName"),
FirstName = GetProperty(resEnt, "givenName"),
MiddleName = GetProperty(resEnt, "initials"),
LastName = GetProperty(resEnt, "sn"),
Company = GetProperty(resEnt, "company"),
JobTitle = GetProperty(resEnt, "title"),
Email = GetProperty(resEnt, "mail"),
Phone = GetProperty(resEnt, "telephoneNumber"),
ExtensionAttribute1 = GetProperty(resEnt, "extensionAttribute1")
};
}
Apprezzo che tu abbia condiviso questo. –
Sto sviluppando su un laptop non appartenente al dominio. Posso passare tale richiesta ad AD? –
Se gli utenti sono locali sulla macchina, è possibile utilizzare ContextType.Machine anziché ContextType.Domain per focalizzare le query sull'archivio autorizzazioni locale. – tvanfosson