Share via

Getting UPN from wpf application

SeekAzure 1 Reputation point
2022-08-12T14:12:50.657+00:00

I have an WPF application which needs to get the User principal Name to do some authentications.
I am using .Net core 3.1 .

How can I do it ?

I have tried using System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName;

but this always throws exception of type

System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.
Source=System.DirectoryServices.AccountManagement
StackTrace:
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.get_Current()

Microsoft Security | Microsoft Entra | Microsoft Entra ID
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.