Fadi Banna
Fadi Banna

Reputation: 554

Windows Universal App Get username from Microsoft Account Authentication

So I wanted the users to login to my app using Microsoft Account I did all the setup in my mobile service in Azure and this is how I implement the login in my App:

    private async Task<bool> AuthenticateAsync()
    {
        string message;
        bool success = false;
        try
        {
            user = await App.MobileService
             .LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);
            message =
                string.Format("You are now signed in - {0}", user.UserId);

            success = true;
        }
        catch (InvalidOperationException)
        {
            message = "You must log in. Login Required";
        }

        var dialog = new MessageDialog(message);
        dialog.Commands.Add(new UICommand("OK"));
        await dialog.ShowAsync();
        return success;
    }

all is working fine but all I get from this is a User Id.

and I need the name of the user that logged in, can anyone help me how should I go about this?

Thanks

Upvotes: 1

Views: 417

Answers (1)

Franklin Chen - MSFT
Franklin Chen - MSFT

Reputation: 4923

and I need the name of the user that logged in, can anyone help me how should I go about this

For UWP app, this is impossible using official managed API. See MobileServiceAuthentication class in here

internal async Task<MobileServiceUser> LoginAsync()
{
        string response = await this.LoginAsyncOverride();
        if (!string.IsNullOrEmpty(response))
        {
            JToken authToken = JToken.Parse(response);

            // Get the Mobile Services auth token and user data
            this.Client.CurrentUser = new MobileServiceUser((string)authToken["user"]["userId"]);
            this.Client.CurrentUser.MobileServiceAuthenticationToken = (string)authToken[LoginAsyncAuthenticationTokenKey];
        }

        return this.Client.CurrentUser;
}

The official sdk just retrieves the userId and MobileServiceAuthenticationToken, for other platform, we need to use GetIdentitiesAsync() method to get identity, see How to get user name, email, etc. from MobileServiceUser? or LINK

The username info actually has been retrieved in the SSO process: enter image description here

So you have to implement the auth process(Extend the method based on the open source code) and maintain the username information as you need.

If you can get the user's input, maybe you can also call Live API: https://msdn.microsoft.com/en-us/library/office/dn659736.aspx#Requesting_info

Upvotes: 2

Related Questions