简体   繁体   中英

UWP : Get the current UserName to provide rights to the user

For an app that I develop for a customer, I need to get the current user details. Then I must assign rights by comparing the couple " domain\\login " to a table containing all the authorized users.

I reuse the code given by @Sergiu Cojocaru here , that I have mixed with an official sample ( UserInfo ) to get more details:

IReadOnlyList<Windows.System.User> users = await Windows.System.User.FindAllAsync();

var current = users.Where(u => u.AuthenticationStatus == Windows.System.UserAuthenticationStatus.LocallyAuthenticated &&
                               u.Type == Windows.System.UserType.LocalUser).FirstOrDefault();

// user may have username
var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
string displayName = (string)data;

// or may be authenticated using hotmail/outlook
if (string.IsNullOrEmpty(displayName))
{
    string firstName = (string)await current.GetPropertyAsync(KnownUserProperties.FirstName);
    string lastName = (string)await current.GetPropertyAsync(KnownUserProperties.LastName);
    displayName = string.Format("{0} {1}", firstName, lastName);
}

But actually I only get the " firstName " and the " lastName " from " KnownUserProperties ". The others properties are empties:

  • providerName: null
  • accountName: null
  • guestHost: null
  • principalName: null
  • domainName: null
  • sipUri: null

=> Is this normal?

The problem is that the expected couple " domain\\login " is based on the result of the command line " whoami ", that doesn't give the same result that " firstName " and " lastName ".

=> Is there a way to get the same result in the app?

I specify that I use a hotmail account but the customers accounts must be based on Active Directory: I don't know if this will change anything...

To get information about domain and domain user change

var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);

to

var data = await current.GetPropertyAsync(KnownUserProperties.DomainName);

on my side result is: ROM.EUROPE.TECHTEAM.COM\\scojocar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM