我需要返回用户所属的所有 Active Directory 组,但在字符串 [ ] 中,因此我可以在通用主体中使用结果。
我不确定是否要投出结果?请帮忙!
string[] roles = new string[] {
helper.GetActiveDirectoryGroups(User.Identity.Name) };
GenericPrincipal principal = new GenericPrincipal(identity,roles);
public string[] GetActiveDirectoryGroups(string userName)
{
//code here
}
最佳答案
这应该可以解决问题。
using System.DirectoryServices.AccountManagement;
public static string[] GetGroups(string username)
{
string[] output = null;
using (var ctx = new PrincipalContext(ContextType.Domain))
using (var user = UserPrincipal.FindByIdentity(ctx, username))
{
if (user != null)
{
output = user.GetGroups() //this returns a collection of principal objects
.Select(x => x.SamAccountName) // select the name. you may change this to choose the display name or whatever you want
.ToArray(); // convert to string array
}
}
return output;
}
关于c# - 在字符串 [] 中返回用户所属的所有 Active Directory 组的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319263/