Unable to cast object of type in System.DirectoryServices.AccountManagement.GroupPrincipal

后端 未结 3 1964
误落风尘
误落风尘 2021-02-07 02:06

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 giv

3条回答
  •  感情败类
    2021-02-07 02:48

    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:

    1. Change the Authentication to "Windows Authentication" (how-to)

    2. Instead of using current, doing it in two steps: (source)

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, User.Identity.Name);

    And to finally get the name (or any other info), I used user.DisplayName.

提交回复
热议问题