Azure MobileApp自定义身份验证,刷新令牌

Kis*_*ke1 3 authentication azure custom-authentication azure-mobile-services

我需要我的应用程序来支持针对我们的私有数据库的自定义身份验证,并按照Adrian Hall书中的建议在这里https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter2/custom/验证用户身份没有问题。当我需要刷新访问令牌时,问题就来了。由于我不得不使用自定义身份验证,因此我有以下问题:

1)我应该调用MobileServicesClient.RefreshUserAsync(),如果是,应该在服务器上实现哪种终结点?每次都会重新发行另一个令牌,使旧令牌失效吗?什么时候应该进行刷新调用?

2)我已经读过有关使用永不过期的刷新令牌的信息,但我找不到真正的示例实现,也找不到关于如何在自定义身份验证方案中实现它的说明,有人可以向我指出正确的方向吗?

提前谢谢了

Bru*_*hen 5

我应该调用MobileServicesClient.RefreshUserAsync()吗?如果是,应该在服务器上实现哪种终结点?每次都会重新发行另一个令牌,使旧令牌失效吗?什么时候应该进行刷新调用?

我已经检查了针对端点发送获取请求的方法RefreshUserAsyncMicrosoft.WindowsAzure.Mobile.dll该方法/.auth/refresh用于使用登录用户的提供程序刷新访问令牌。由于您使用的是自定义身份验证,因此无法使用此方法刷新authenticationToken

我已经读过有关使用永不过期的刷新令牌的信息,但是我找不到真正的示例实现,也找不到关于如何在自定义身份验证方案中实现它的说明,有人可以向我指出正确的方向吗?

使用AppServiceLoginHandler中CreateToken方法时,可以指定as ,然后将检索永不过期的方法如下:lifetimenullauthenticationToken

JwtSecurityToken token = AppServiceLoginHandler.CreateToken(claims, signingKey, audience, issuer,null);
Run Code Online (Sandbox Code Playgroud)

此外,您可以尝试构建端点以基于旧的有效令牌创建新令牌,如下所示:

[Route(".auth/login/customRefreshToken")]
public IHttpActionResult RefreshToken([FromBody] RefreshTokenInput body)
{
    string tokenString = body.AuthenticationToken;
    try
    {
        var jwtSecurityToken = new JwtSecurityToken(tokenString);
        JwtSecurityToken token = AppServiceLoginHandler.CreateToken(jwtSecurityToken.Claims, signingKey, audience, issuer, TimeSpan.FromDays(30));
        return Ok(new LoginResult()
        {
            AuthenticationToken = token.RawData
        });
    }
    catch (Exception e)
    {
        return BadRequest("$Error = {e.Message}, StackTrace = {e.StackTrace}");
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:对于您的移动客户端,您可以MobileServiceClient.InvokeApiAsync用来检索新令牌,然后解析authenticationToken并将其更新为MobileServiceClient.CurrentUser.MobileServiceAuthenticationToken

结果

在此处输入图片说明

在此处输入图片说明