Search code examples
windows-store-appsmicrosoft-account

Username in WebTokenRequestResult is empty


in a Windows 10 UWP I try use WebAuthenticationCoreManager.RequestTokenAsync to get the result from a login with a Microsoft account. I get a WebTokenRequestResult with Success. ResponseData[0] contains a WebAccount with an ID - but the UserName is empty. The scope of the call is wl.basic - so I should get a lot of information... I'm not sure how to retrieve extra information - and for the current test the Username would be OK.

I checked out the universal samples - and there I found a snippet which tries to do what I'm trying - an output of webTokenRequestResult.ResponseData[0].WebAccount.UserName.

By the way - the example output is also empty.

Is this a bug - or what do I (and the MS in the samples) have to do to get the users profile data (or at least the Username)?


Solution

  • According to the documentation (https://learn.microsoft.com/en-us/windows/uwp/security/web-account-manager), you have to make a specific REST API call to retrieve it:

    var restApi = new Uri(@"https://apis.live.net/v5.0/me?access_token=" +     result.ResponseData[0].Token);
    
    using (var client = new HttpClient())
    {
        var infoResult = await client.GetAsync(restApi);
        string content = await infoResult.Content.ReadAsStringAsync();
    
        var jsonObject = JsonObject.Parse(content);
        string id = jsonObject["id"].GetString();
        string name = jsonObject["name"].GetString();
    }
    

    As to why the WebAccount property doesn't get set... shrugs

    And FYI, the "id" returned here is entirely different from the WebAccount.Id property returned with the authentication request.