Active Directory kullanıcı grupları için? (c# asp.net)
Bu kod, geçerli kullanıcı grupları için kullanıyorum. Ama el ile kullanıcı vermek ve onun grupları dönmek istiyorum. Bunu nasıl yapabilirim?
using System.Security.Principal;
public ArrayList Groups()
{
ArrayList groups = new ArrayList();
foreach (IdentityReference group in System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
{
groups.Add(group.Translate(typeof(NTAccount)).ToString());
}
return groups;
}
CEVAP
Eğer .NET ya da 3.5, System.DirectoryServices.AccountManagement
new kullanabilirsiniz (S. DS.Eskisinden AM) bu çok daha kolay hale getirir ad.
Hepsini burada okuyun:
Güncelleme:eski MSDN dergi makaleleri artık, ne yazık ki online değil - Microsoft download the CHM for the January 2008 MSDN magazine ve orada makaleyi okumak gerekir.
Temel olarak, bir olması gerekir "asıl bağlam" (genellikle etki), kullanıcı bir ana, ve sonra kendi gruplarına almak çok kolay:
public List<GroupPrincipal> GetGroups(string userName)
{
List<GroupPrincipal> result = new List<GroupPrincipal>();
// establish domain context
PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
// find your user
UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, userName);
// if found - grab its groups
if(user != null)
{
PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
// iterate over all groups
foreach(Principal p in groups)
{
// make sure to add only group principals
if(p is GroupPrincipal)
{
result.Add((GroupPrincipal)p);
}
}
}
return result;
}
ve hepsi bu kadar! Şimdi sonuç (liste) kullanıcı - onlara, onların adlarını yazdırmak yineleme ait veya ne yapman gerekiyorsa yetki grupları var.
Güncelleme:UserPrincipal
nesne üzerinde yüzeyli olmayan bazı özellikleri, erişim için DirectoryEntry
temel kazmak gerekir:
public string GetDepartment(Principal principal)
{
string result = string.Empty;
DirectoryEntry de = (principal.GetUnderlyingObject() as DirectoryEntry);
if (de != null)
{
if (de.Properties.Contains("department"))
{
result = de.Properties["department"][0].ToString();
}
}
return result;
}
Güncelleme #2:bu iki kod parçacıkları bir araya koymak çok zor değil gibi görünüyor.... ama Tamam - aynen şöyle:
public string GetDepartment(string username)
{
string result = string.Empty;
// if you do repeated domain access, you might want to do this *once* outside this method,
// and pass it in as a second parameter!
PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain);
// find the user
UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, username);
// if user is found
if(user != null)
{
// get DirectoryEntry underlying it
DirectoryEntry de = (user.GetUnderlyingObject() as DirectoryEntry);
if (de != null)
{
if (de.Properties.Contains("department"))
{
result = de.Properties["department"][0].ToString();
}
}
}
return result;
}
Bir kullanıcı adı ve şifre doğrulamak ...
Nasıl SQL Server login olarak Active D...
Nasıl Dosya Active Directory üzerinde ...
Nasıl bir dosya " Active Directory bir...
Kullanıcı, Grup ve Rol Yönetimi .Activ...