简体   繁体   English

C# Active Directory PrincipalContext / UserPrincipal.IsMemberOf 错误

[英]C# Active Directory PrincipalContext / UserPrincipal.IsMemberOf error

So I have a question I'm honestly not quite sure how to ask.所以我有一个问题,老实说,我不太确定该怎么问。 Essentially I have a bit of code that works fantastically on my local machine when I run it.基本上,当我运行它时,我有一些代码在我的本地机器上运行得非常好。 Once I publish it to our development web server, it fails.一旦我将它发布到我们的开发 Web 服务器,它就会失败。 I'm not sure if it's an IIS setup issue, web.config issue or a coding issue.我不确定这是 IIS 设置问题、web.config 问题还是编码问题。

Here's the snippet of code这是代码片段

    bool isMember = false;

    PrincipalContext ADDomain = new PrincipalContext(ContextType.Domain);
    UserPrincipal user = UserPrincipal.FindByIdentity(ADDomain, userID);

    if (user.IsMemberOf(ADDomain, IdentityType.Name, groupName.Trim()))
    {
        isMember = true;
    }

    return isMember;

Where I pass in a user name and a group and it tells me if that user's a member in that group.我传入用户名和组的地方,它告诉我该用户是否是该组的成员。 No problem.没问题。 Works great on my machine.在我的机器上运行良好。 I went to publish that code to the webserver and it fails when it hits the line我去将该代码发布到网络服务器,但它在上线时失败了

UserPrincipal user = UserPrincipal.FindByIdentity(ADDomain, userID); 

it throws this error:它抛出这个错误:

[DirectoryServicesCOMException (0x80072020): An operations error occurred.] [DirectoryServicesCOMException (0x80072020): 发生操作错误。]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +788 System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +788
System.DirectoryServices.DirectoryEntry.Bind() +44 System.DirectoryServices.DirectoryEntry.Bind() +44
System.DirectoryServices.DirectoryEntry.get_AdsObject() +42 System.DirectoryServices.DirectoryEntry.get_AdsObject() +42
System.DirectoryServices.PropertyValueCollection.PopulateList() +29 System.DirectoryServices.PropertyValueCollection.PopulateList() +29
System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +63 System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +63
System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +163 System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +521217 System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +163 System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +521217
System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +51 System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +51
System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +141 System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +141
System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +42 System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +42
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) +29 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) +29
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +95 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +95
Cosmic.Web.Login.btnSubmit_Click(Object sender, EventArgs e) in C:\\cosmic\\Cosmic.Web\\Login.aspx.cs:79 Cosmic.Web.Login.btnSubmit_Click(Object sender, EventArgs e) 在 C:\\cosmic\\Cosmic.Web\\Login.aspx.cs:79
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3691 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3691

Any ideas where this could be failing?这可能会失败的任何想法?

My first guess would be: that user account you're running this code under doesn't have the necessary permissions to query Active Directory.我的第一个猜测是:您在其下运行此代码的用户帐户没有查询 Active Directory 的必要权限。

To fix this, basically you need to change your constructor from this:要解决这个问题,基本上你需要改变你的构造函数:

PrincipalContext ADDomain = new PrincipalContext(ContextType.Domain);

(establishes a connection to AD with the current, default credentials this code is running under) (使用此代码运行的当前默认凭据建立与 AD 的连接)

to this:对此:

PrincipalContext ADDomain = 
   new PrincipalContext(ContextType.Domain, "DOMAIN", useraccount, password);

and provide a username and password for a user account that you know has sufficient privileges to query Active Directory.并为您知道有足够权限查询 Active Directory 的用户帐户提供用户名和密码。

If you've got several seconds to spare waiting for your data form a large AD, then go ahead and use PrincipalContext but if you want your response in milliseconds, use DirectoryEntry, DirectorySearcher and .PropertiesToLoad.如果您有几秒钟的时间等待来自大型 AD 的数据,请继续使用 PrincipalContext,但如果您希望以毫秒为单位进行响应,请使用 DirectoryEntry、DirectorySearcher 和 .PropertiesToLoad。

Here's an example这是一个例子

https://stackoverflow.com/a/65986796/5248400 https://stackoverflow.com/a/65986796/5248400

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#System.DirectoryServices.AccountManagement未知错误(0x80005000)UserPrincipal.IsMemberOf() - C# System.DirectoryServices.AccountManagement Unknown error (0x80005000) UserPrincipal.IsMemberOf() UserPrincipal.IsMemberOf返回false - UserPrincipal.IsMemberOf is returning false C#Active Directory主体上下文权限 - C# Active Directory Principalcontext Permissions 在 Active Directory C# 中创建用户的 PrincipalContext 或 DirectoryEntry 哪个更好 - Which is better PrincipalContext or DirectoryEntry for user creation in Active Directory C# 如何使用 C# 中的 UserPrincipal 获取 Active Directory 中的地址详细信息 - How to get details of address in Active Directory using UserPrincipal in C# 使用PrincipalContext搜索Active Directory组时,不会返回某些UserPrincipal属性 - When Searching Active Directory Groups Using PrincipalContext Certain UserPrincipal Properties are not Returned UserPrincipal.FindByIdentity(PrincipalContext context, string identityValue) 如何查询 Active Directory? - How does UserPrincipal.FindByIdentity(PrincipalContext context, string identityValue) query Active Directory? 使用 PrincipalContext 连接到活动目录 - Connect to active directory with PrincipalContext 用于在Active Directory中查询的PrincipalContext - PrincipalContext for query in Active Directory 如何使用ASP.net,C#通过PrincipalContext在活动目录上添加Manager属性 - How to add Manager attribute on active directory by PrincipalContext using ASP.net,C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM