[英]DirectoryServicesCOMException on IIS7 with System.DirectoryServices.AccountManagement but not in dev
[英]DirectoryServicesCOMException when working with System.DirectoryServices.AccountManagement
我正在嘗試使用System.DirectoryServices.AccountManagment確定用戶是否是給定組的成員。
我能夠實例化PrincipalContext
:
PrincipalContext pc = new PrincipalContext(ContextType.Domain)
接下來,我試着抓住一個校長:
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, "MYDOMAIN\somegroup");
// snip: exception thrown by line above.
}
以上和UserPrincipal.FindByIdentity
與用戶SAM一起拋出DirectoryServicesCOMException
:“登錄失敗:未知用戶名或密碼錯誤”
我已經嘗試將完整的SAMAccountName傳遞給FindByIdentity
(以MYDOMAIN \\用戶名的形式)或只是用戶名而行為沒有變化。 我嘗試使用HostingEnvironment.Impersonate
和SPSecurity.RunWithElevatedPrivileges
方法使用其他憑據執行代碼,並且也會遇到相同的結果。
我還嘗試使用域名實例化我的上下文:
Principal Context pc = new PrincipalContext(ContextType.Domain, "MYDOMAIN");
這會拋出PrincipalServerDownException
:“無法聯系服務器。”
我正在研究一台相當硬化的服務器。 我沒有鎖定系統,所以我不確定它到底做了什么。 如果我需要為我的池標識用戶或域安全策略分配憑據以使這些憑據有效,我可以相應地配置域。 是否有任何設置會阻止我的代碼運行? 我在代碼本身中遺漏了什么嗎? 這在SharePoint網站中是不可能的嗎?
編輯:經過進一步測試,我的代碼在面向.NET 4.0的控制台應用程序中測試時正常運行。 我針對不同的框架,因為在針對.NET 3.5出於某種原因我沒有在控制台應用程序中使用AccountManagement。
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
using (UserPrincipal adUser = UserPrincipal.FindByIdentity(pc, "MYDOMAIN\joe.user"))
using (GroupPrincipal adGroup = GroupPrincipal.FindByIdentity(pc, "MYDOMAIN\user group"))
{
if (adUser.IsMemberOf(adGroup))
{
Console.WriteLine("User is a member!");
}
else
{
Console.WriteLine("User is NOT a member.");
}
}
在我的SharePoint環境中可能會禁止此功能執行的內容有何不同?
我將IIS應用程序池使用的帳戶添加到Administrators組,此問題已解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.