C# Windows通用应用程序从Microsoft帐户身份验证获取用户名

C# Windows通用应用程序从Microsoft帐户身份验证获取用户名,c#,uwp,microsoft-account,C#,Uwp,Microsoft Account,因此,我希望用户使用Microsoft帐户登录到我的应用程序 我在Azure的移动服务中完成了所有设置,以下是我在应用程序中实现登录的方式: private async Task<bool> AuthenticateAsync() { string message; bool success = false; try { user = await App.MobileService

因此,我希望用户使用Microsoft帐户登录到我的应用程序 我在Azure的移动服务中完成了所有设置,以下是我在应用程序中实现登录的方式:

    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;
    }
private异步任务AuthenticateAsync()
{
字符串消息;
布尔成功=假;
尝试
{
用户=等待App.MobileService
.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);
信息=
Format(“您现在已登录-{0}”,user.UserId);
成功=真实;
}
捕获(无效操作异常)
{
message=“您必须登录,需要登录”;
}
var dialog=新建消息对话框(消息);
添加(新的UICommand(“OK”);
wait dialog.ShowAsync();
回归成功;
}
一切正常,但我从中得到的只是一个用户Id

我需要登录用户的姓名,有人能帮我怎么做吗

谢谢

我需要登录用户的姓名,有人能帮我怎么做吗

对于UWP应用程序,使用官方托管API是不可能的。请参见中的
MobileServiceAuthentication
class

内部异步任务LoginAsync()
{
string response=wait this.LoginAsyncOverride();
如果(!string.IsNullOrEmpty(响应))
{
JToken authToken=JToken.Parse(响应);
//获取移动服务身份验证令牌和用户数据
this.Client.CurrentUser=新的MobileServiceUser((字符串)authToken[“user”][“userId”]);
this.Client.CurrentUser.MobileServiceAuthenticationToken=(字符串)authToken[LoginAsyncAuthenticationTokenKey];
}
返回this.Client.CurrentUser;
}
官方sdk只检索用户ID和MobileServiceAuthenticationToken,对于其他平台,我们需要使用
getIdentitesAsync()
方法获取标识,请参见或

用户名信息实际上已在SSO过程中检索到:

因此,您必须实现auth过程(基于开放源代码扩展该方法)并根据需要维护用户名信息


如果你能得到用户的输入,也许你也可以调用Live API:

谢谢你提供了一个非常详细的答案
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;
}