跨 UWP 应用程序和 Azure 移动服务的持久身份验证
Persistent authentication across UWP app and Azure Mobile Service
基于示例 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);
}
}
正如我在评论中提到的,我得到的只是 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吗?
仍然离解决方案还很远,所以感谢您的帮助。
任何人都可以随时纠正我,但 RequestTokenAsync 似乎为您提供了一个 访问令牌 ,您不能使用它来登录后端。为此,您需要一个 身份验证令牌 ,据我所知,RequestTokenAsync 无法满足您的要求。
有一些关于令牌的信息here。
如果人们最终在这里搜索 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");
}
希望它可以节省一些时间!
基于示例 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);
}
}
正如我在评论中提到的,我得到的只是 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吗?
仍然离解决方案还很远,所以感谢您的帮助。
任何人都可以随时纠正我,但 RequestTokenAsync 似乎为您提供了一个 访问令牌 ,您不能使用它来登录后端。为此,您需要一个 身份验证令牌 ,据我所知,RequestTokenAsync 无法满足您的要求。
有一些关于令牌的信息here。
如果人们最终在这里搜索 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");
}
希望它可以节省一些时间!