【问题标题】:Persistent authentication across UWP app and Azure Mobile Service跨 UWP 应用和 Azure 移动服务的持久身份验证
【发布时间】:2016-05-24 20:28:34
【问题描述】:

在示例here 的基础上,我尝试在客户端验证 MSA 登录,并让它在服务端进行验证。与我的不同之处在于我在 Windows 10 中使用新的 WebAccount 相关 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);
    }
}

正如我在 cmets 中提到的,我得到的只是 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 吗?

仍然没有更接近解决方案,因此感谢您的帮助。

【问题讨论】:

    标签: c# azure authentication azure-mobile-services uwp


    【解决方案1】:

    任何人都可以随时纠正我,但看起来 RequestTokenAsync 为您提供了一个 访问令牌,您不能使用它来登录后端。为此,您需要一个 身份验证令牌,据我所知,RequestTokenAsync 并不能满足您的要求。

    有一些关于令牌的信息here

    【讨论】:

    • 这是正确的。移动应用尚未针对 UWP 进行优化 - SDK 面向 8.1,可用于 UWP 项目,但尚未进行任何更改以适应新功能。我们将为此进行更新。
    【解决方案2】:

    如果人们最终在这里寻找 App Service Mobile 的解决方案,请更新 MobileService。然后现在有一个solution

    这里复制的代码是:

    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");
    }
    

    希望它能节省一些时间!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      相关资源
      最近更新 更多