我正在UserPrincipal.Current.ToString()域中使用方法来获取具有有效域的域用户当前记录.但当我在一个字符串中显示它时,它在IIS服务器中托管时给出错误:
Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal'
to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.
Run Code Online (Sandbox Code Playgroud)
小智 30
我有同样的问题.它在我的本地计算机上运行良好,但在将其部署到服务器上的IIS时失败了.最后我不得不改变两件事来使其发挥作用:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, User.Identity.Name);
最后得到了名字(或任何其他信息),我用过user.DisplayName.
在Windows 7上的IIS 7下运行时,我看到了此异常.
System.Security.Principal.WindowsIdentity.GetCurrent().Name返回"IIS APPPOOL\ASP.NET v4.0".
这不是真正的用户帐户,这部分解释了正在发生的事情,尽管恕我直言UserPrincipal.Current应该更优雅地处理这种情况.
我认为这是一个错误,并在Connect上创建了一个错误:
作为解决方法,用于System.Security.Principal.WindowsIdentity.GetCurrent()获取IIS AppPool的标识.
| 归档时间: |
|
| 查看次数: |
18438 次 |
| 最近记录: |