问题
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...
回答1:
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
来源:https://stackoverflow.com/questions/40179695/uwp-get-the-current-username-to-provide-rights-to-the-user