問題描述
PrincipalContext context = new PrincipalContext(ContextType.Domain, "ipofmachine", "DC=xyz,DC=org", "username", "Password");
UserPrincipal userPrinciple = UserPrincipal.FindByIdentity(context, "User0");
var groups = userPrinciple.GetAuthorizationGroups();
if (userPrinciple != null)
{
foreach (GroupPrincipal gp in groups)
{
//some thing
}
}
我需要給予任何許可嗎?在一些博客中,我了解到如果沒有設置為包含 SID 歷史記錄的用戶,那么這將正常工作(但我認為您無法編輯組的 sid 值)
Is there any permission that I need to give? In some of the blogs I learned that if there are no users which are set to include the SID history then this will work fine (but i think you can not edit the sid values of the groups)
推薦答案
我發現將域用戶添加到本地組時存在問題,但后來該域用戶從 Active Directory 中刪除.該本地組的狀態是使用 SID 而不是顯示為成員的域用戶名.
I've found there's an issue when you add a domain user to a local group, but later that domain user is deleted out of Active Directory. The state of that local group is that instead of a domain username showing up as a member, the SID is used instead.
但是!
該 SID 不再存在于 Active Directory 中,這導致事情變得繁榮起來.
That SID doesn't exist in Active Directory anymore causing things to go boom.
當然,彈出 NoMatchingPrincipalException 的原因可能有很多,因此此代碼提供了一種解決方法.它來自 MSDN 上的一篇很棒的帖子.下面的代碼是在這里找到的修改版本:
Of course there could be many other reasons for an NoMatchingPrincipalException to pop up, so this code provides a workaround for that. It comes from a terrific post on MSDN. The code below is a modified version found here:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/9dd81553-3539-4281-addd-3eb75e6e4d5d/getauthorizationgroups-fails-with-nomatchingprincipalexception
public static IEnumerable<Principal> getAuthorizationGroups(UserPrincipal user)
{
PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
List<Principal> ret = new List<Principal>();
var iterGroup = groups.GetEnumerator();
using (iterGroup)
{
while (iterGroup.MoveNext())
{
try
{
Principal p = iterGroup.Current;
Console.WriteLine(p.Name);
ret.Add(p);
}
catch (NoMatchingPrincipalException pex)
{
continue;
}
}
}
return ret;
}
這篇關于GetAuthorizationGroups() 拋出異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!