无法转换类型为System.DirectoryServices.AccountManagement.GroupPrincipal的对象 [英] Unable to cast object of type in System.DirectoryServices.AccountManagement.GroupPrincipal
问题描述
我正在使用Domain中的方法UserPrincipal.Current.ToString()
来获取具有有效域的当前登录域用户.但是当我以字符串显示它时,在IIS服务器中托管时会产生错误:
I am Using Method UserPrincipal.Current.ToString()
in Domain to Get Current Logged in Domain User with Valid Domain. but when i am Displaying it in a string its giving Error when hosted in IIS Server:
Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal'
to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.
推荐答案
我遇到了同样的问题.它在我的本地计算机上运行良好,但是在将其部署到服务器上的IIS时失败了.最后,我必须更改两件事以使其起作用:
I had the same problem. It worked perfectly on my local machine but when deployed it to IIS on the server it failed. In the end I had to change two things to make it work:
-
将身份验证更改为"Windows身份验证"(如何-到)
Instead of using current, doing it in two steps: (source)
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, User.Identity.Name);
为了最终获得名称(或其他信息),我使用了user.DisplayName
.
And to finally get the name (or any other info), I used user.DisplayName
.
这篇关于无法转换类型为System.DirectoryServices.AccountManagement.GroupPrincipal的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!