System.DirectoryServices& IIS - 获取用户名

时间:2011-11-15 14:45:24

标签: asp.net-mvc ldap directoryservices

我不确定这是否会进入系统管理员领域太远,但无论如何我都会问我的问题:)

我正在开发一个MVC3网站。该站点使用约束委派来模拟Windows域中的用户并连接到该用户下的SQL Server。这一切都没有问题。

我要做的是访问登录用户名的Display Name属性(使用以下代码):

string displayName;
var context = new PrincipalContext(ContextType.Domain, "contoso");
var userPrincipal = UserPrincipal.FindByIdentity(context,
                                  IdentityType.SamAccountName,
                                  userName);
displayName = userPrincipal.DisplayName;

我收到LDAP操作错误:

  

异常详细信息:System.Runtime.InteropServices.COMException:发生操作错误   System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext   context,IdentityType identityType,String identityValue)

奇怪的是:

  • 此操作在验收测试服务器上成功(相同配置,相同域,相同应用程序池用户,不同机器)。
  • 使用远程桌面连接到运行ASP.NET应用程序池的用户下的生产计算机后,此操作在控制台应用程序中成功。
  • 操作从具有普通域帐户的随机工作站成功。
  • 生产服务器上的网站中的操作失败。

1 个答案:

答案 0 :(得分:0)

我通过将网站的PhysicalPathCredentials设置为帐户来解决此问题。但我无法弄清楚问题的原因,但它确实有效。