Last post May 08, 2019 09:15 AM by Wei Zhang
May 07, 2019 03:43 PMfirstname.lastname@example.org|LINK
I have a piece of visual basic code which runs fine in my local machine and displays UPN on the screen.
Dim uID1 As String = UserPrincipal.Current.UserPrincipalName.ToString() // to retrieve UPN from Active Directory
However, when I publish the same code in IIS on the UAT server, the code throws an exception as per below
"Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrinicpal to type System.DirectoryServices.AccountManagement.UserPrincipal"
I am suspecting its due to the
Imports System.DirectoryServices.AccountManagement statement, which is not valid in
.NET 2.0 which is my UAT server .NET version.
Can help me on the workaround if any, on the below exception ? Looking forward to hear from you. Thanks.
May 07, 2019 05:14 PM|mgebhard|LINK
In web apps the user identity is User.Identity.Name.
This assume you've configured the IIS app to use Windows Authentication.
May 08, 2019 02:16 AM|Wei Zhang|LINK
UserPrincipal.Current.ToString() worked perfectly on my local machine but when deployed it to IIS on the server it failed. I suggest that you could do two things as below: so that it will work well in iis.
1.Change the Authentication to "Windows Authentication".Here is the link for how to change, I hope it could help you.
2.Instead of using current, doing it as below:
Private Sub SurroundingSub()
Dim ctx As PrincipalContext = New PrincipalContext(ContextType.Domain)
Dim user As UserPrincipal = UserPrincipal.FindByIdentity(ctx, User.Identity.Name)
And to finally get the name (or any other info), you could use user.DisplayName.
Here is the link, I hope it could help you.
May 08, 2019 06:12 AMemail@example.com|LINK
Hi Zhang Wei,
Thanks a lot for your response. I am puzzled if it will work in .NET 2.0 which is my UAT version.
May 08, 2019 09:15 AM|Wei Zhang|LINK
Sorry for my fault,it seems that if your .net framework version is lower than 3.5 ,it couldn't work.However I find a workaround which is using WindowsIdentity.GetCurrent（）.I hope it could help you.
Here is the link.