以下代码在我的开发电脑上运行良好,但在服务器部署时崩溃:
以下是崩溃信息:
using System.DirectoryServices.AccountManagement;
using (var ctx = new PrincipalContext(ContextType.Domain, domainName))
using (GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName))
{
if (grp == null) return new string[0];
return grp.GetMembers(true).Select(m => m.SamAccountName).ToArray();
}
以下是崩溃信息:
[DirectoryServicesCOMException (0x80072020): An operations error occurred.]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +439513
System.DirectoryServices.DirectoryEntry.Bind() +36
System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
System.DirectoryServices.PropertyValueCollection.PopulateList() +22
System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +96
System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +141
System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +1134
System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +37
System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +124
System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +31
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) +14
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue) +86
System.DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue) +29
WebApp.WebForm1.Test() in C:\MyProject\trunk\WebApp\WebForm1.aspx.cs:30
情景:
- IIS7 运行在加入域的 Windows Server 2008 x64 上
- ASP.NET 4 内部网应用程序
- IIS 中所有身份验证模式关闭,除了集成的 Windows 身份验证
- 应用程序池被配置为运行为域用户
Web.config 如下:
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
<identity impersonate="true" >
System.Security.Principal.WindowsIdentity.GetCurrent().Name
返回我的 AD 用户名,在生产环境中是正确的,因此我认为这意味着模拟用户(impersonation)已经正常工作。