What is the MAUI equivalent of get_UserUpn?

41 Views Asked by At

In a MAUI app, what is the full equivalent of IAzClientContext::get_UserUpn where a UPN is retrieved in the full content, even if it is in email syntax like [email protected]?

I have tried System.Security.Principal.WindowsIdentity.GetCurrent().Name & WindowsIdentity(userToken) and variations with no success. Most other functions that I have found or been told about are based on the Win32 libraries, which would bypass the MAUI cross-platform goal.

1

There are 1 best solutions below

1
Shashank R On

there isn't a direct equivalent method named get_UserUpn. However, to achieve similar functionality, you can use platform-specific APIs or dependency injection to access the user's UPN (User Principal Name) on each platform.

For example, on platforms like Android and iOS, you might need to use platform-specific APIs to retrieve the user's UPN, as it's not typically exposed directly through cross-platform APIs.

You can use Android's AccountManager to retrieve the user's UPN:

C#,

public string GetUserUpn()
    {
        var accountManager = 
        Android.Accounts.AccountManager.Get(Context);
        var accounts = accountManager.GetAccountsByType("com.google");
        if (accounts.Length > 0)
            {
                return accounts[0].Name; // This may or may not be the UPN, depending on the account type and user settings
            }
        return null;
    }