UWP : Get the current UserName to provide rights to the user
<p>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 "<strong>domain\login</strong>" to a table containing all the authorized users.</p>

<p>I reuse the code given by @Sergiu Cojocaru <a href="https://stackoverflow.com/questions/33394019/get-username-in-a-windows-10-c-sharp-uwp-universal-windows-app/33395034#33395034">here</a> , that I have mixed with an official sample (<a href="https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/UserInfo" rel="nofollow noreferrer">UserInfo</a>) to get more details:</p>

<pre><code>IReadOnlyList&lt;Windows.System.User&gt; users = await Windows.System.User.FindAllAsync();

var current = users.Where(u =&gt; u.AuthenticationStatus == Windows.System.UserAuthenticationStatus.LocallyAuthenticated &amp;&amp;
                               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);
}
</code></pre>

<p>But actually I  only get the "<strong>firstName</strong>" and the "<strong>lastName</strong>" from "<strong>KnownUserProperties</strong>". The others properties are empties:</p>

<ul>
<li>providerName: null</li>
<li>accountName: null</li>
<li>guestHost: null</li>
<li>principalName: null</li>
<li>domainName: null</li>
<li>sipUri: null</li>
</ul>

<p><strong><em>=> Is this normal?</em></strong></p>

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

<p><strong><em>=> Is there a way to get the same result in the app?</em></strong></p>

<p>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...</p>