简体   繁体   English

使用JWT令牌使访问令牌过期时刷新令牌调用

[英]Refresh Token call when Access token expired using JWT token

I am creating two web application in .net core, entity framework db first approach and another web application in .net core only for client side using razor page. 我在.net核心中创建两个Web应用程序,第一种是实体框架db方法,而在.net核心中创建另一个Web应用程序,仅针对使用剃刀页面的客户端。 My problem is i am keeping my api method as authorize and generating access token and refresh token once user is login. 我的问题是我将api方法保持为授权状态,并在用户登录后生成访问令牌和刷新令牌。 My access token is expired in 20 minutes after that i have to call refresh token silently.I am not getting where to call refresh token so that whenever my access token expired it will call refresh token. 我的访问令牌在20分钟后过期,因此我必须静静地调用刷新令牌。我无法在哪里调用刷新令牌,因此无论何时我的访问令牌过期,它都会调用刷新令牌。

In ajax error block i am calling refresh token, one it will generate new token, then after sleep of 5 second am calling again main method. 在ajax错误块中,我正在调用刷新令牌,它会生成一个新令牌,然后在睡眠5秒后再次调用main方法。 If status code is 200 then return data.But that method every time returning 401 status code. 如果状态码为200,则返回数据,但是该方法每次返回401状态码。

  error: function (request, textStatus, errorThrown) {
               ErrorHandleFunction(request, textStatus, errorThrown);
               sleep(5000).then(() => {
                    Myfunction();
                });

In error when it will go then call the refresh token, after that it will go my function, so that again expiry of access token should be set as 20 minute.Currently it is looping in refresh token call . 错误时它将调用刷新令牌,然后它将调用我的函数,因此再次将访问令牌的到期时间设置为20分钟。当前它正在刷新令牌调用中循环。

You need to implement logic to generate refresh tokens yourself in your WebApi. 您需要实现自己在WebApi中生成刷新令牌的逻辑。 When you make a request to the WebApi you should get a 401 Unauthorized. 当您向WebApi提出请求时,您应该获得401 Unauthorized。 Maybe a header record to explain that the token has expired? 也许是头记录来解释令牌已过期? That way you'll know on the front end that you need to call your "RefreshToken" endpoint with your expired token, to get the refresh token. 这样,您将在前端知道需要使用过期的令牌来调用“ RefreshToken”端点,以获取刷新令牌。

Here is an example: https://www.blinkingcaret.com/2018/05/30/refresh-tokens-in-asp-net-core-web-api/ 这是一个示例: https : //www.blinkingcaret.com/2018/05/30/refresh-tokens-in-asp-net-core-web-api/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM