【发布时间】:2012-06-13 12:01:26
【问题描述】:
我在域中使用方法UserPrincipal.Current.ToString() 来获取当前登录的具有有效域的域用户。但是当我在一个字符串中显示它时,它在 IIS 服务器中托管时给出错误:
Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal'
to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.
【问题讨论】:
-
这个错误的原因是由于试图从一个未实例化的对象中读取。在服务器上,您需要先创建并填充 PrincipalContext 对象,然后才能从中读取数据。在调试模式下运行时,VS 会自动创建其中一些类型的对象,以便它们可以在调试器中链接到手表等。