UWP应用程序和Azure移动服务之间的持久身份验证

本文关键字:身份验证 之间 服务 应用程序 Azure 移动 UWP | 更新日期: 2023-09-27 18:26:03

在这里的例子的基础上,我试图对客户端上的MSA登录进行身份验证,并让它对服务端进行身份验证。与我的不同之处在于,我在Windows 10中使用了与WebAccountrelevant API相关的新API,而不是现在不推荐使用的Live SDK。

到目前为止,我有:

var provider = await WebAuthenticationCoreManager.FindAccountProviderAsync("https://login.microsoft.com", "consumers");
var request = new WebTokenRequest(provider, "service::wl.basic wl.emails::DELEGATION", "none");
var result = await WebAuthenticationCoreManager.RequestTokenAsync(request);
if (result.ResponseStatus == WebTokenRequestStatus.Success)
{
    string token = result.ResponseData[0].Token;
    //This calls my custom wrappers around the Live REST API v5 and runs successfully with this token
    var acc = await LiveApi.GetLiveAccount(token);
    var jtoken = new JObject
    {
        {"authenticationToken", token}
    };
    try
    {
        //Shouldn't this work? but raises a 401
        await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount, jtoken);
        //Alternate method? Also raises a 401 
        //await App.MobileService.LoginWithMicrosoftAccountAsync(token);
    }
}

正如我在评论中提到的,我得到的都是401。

据我所知,应用程序在Microsoft帐户开发中心配置正确:

  • 我正在Azure门户中使用来自同一应用程序的客户端ID和机密
  • JWT发行不受限制
  • 重定向URL的格式为https://{appname}.azurewebsites.net/.auth/login/microsoftaccount/callback

当我切换到使用纯服务器端身份验证时,身份验证工作正常。即

await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);

有什么想法吗?我是不是错过了什么?如有任何帮助,我们将不胜感激。

更新时间:我在WebTokenRequestResult中得到的令牌长877个字符,并且似乎不是JWT格式,带有句点(.)分隔符,我很确定这就是问题所在。当客户端调用上面的代码时,以下错误会被登录到服务中:

JWT validation failed: IDX10708: 'System.IdentityModel.Tokens.JwtSecurityTokenHandler' cannot read this string: 'EwCQAq1DBAAUGCCXc8wU/zFu9QnLdZXy+...Zz9TbuxCowNxsEPPOvXwE='.
Application: The string needs to be in compact JSON format, which is of the form: '<Base64UrlEncodedHeader>.<Base64UrlEndcodedPayload>.<OPTIONAL, Base64UrlEncodedSignature>'..
Application: 2015-12-07T17:47:09  PID[5740] Information Sending response: 401.71 Unauthorized

令牌当前的格式是什么?它能变成JWT吗?

仍然没有更接近的解决方案,所以任何帮助都是感谢的。

UWP应用程序和Azure移动服务之间的持久身份验证

任何人都可以纠正我的错误,但看起来RequestTokenAsync为您提供了一个访问令牌,您不能使用该令牌登录后端。为此,您需要一个身份验证令牌,据我所知,RequestTokenAsync并没有为您提供这一点。

这里有一些关于代币的信息。

如果人们最终在这里搜索App Service Mobile的解决方案,则会更新到MobileService。然后现在有一个解决方案

这里复制的代码是:

async Task<string> GetDataAsync()
{
try
{
    return await App.MobileService.InvokeApiAsync<string>("values");
}
catch (MobileServiceInvalidOperationException e)
{
    if (e.Response.StatusCode != HttpStatusCode.Unauthorized)
    {
        throw;
    }
}
// Calling /.auth/refresh will update the tokens in the token store
// and will also return a new mobile authentication token.
JObject refreshJson = (JObject)await App.MobileService.InvokeApiAsync(
    "/.auth/refresh",
    HttpMethod.Get,
    null);
string newToken = refreshJson["authenticationToken"].Value<string>();
App.MobileService.CurrentUser.MobileServiceAuthenticationToken
    = newToken;
return await App.MobileService.InvokeApiAsync<string>("values");
}

希望它能节省一些时间!