无法转换类型为System.DirectoryServices.AccountManagement.GroupPrincipal的对象

编程入门 行业动态 更新时间:2024-10-27 01:37:04
本文介绍了无法转换类型为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的对象

    本文发布于:2023-06-06 02:24:43,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/534748.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:对象   类型   System   DirectoryServices   AccountManagement

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!