加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-web-api2 – 何时将刷新令牌传递给API

发布时间:2020-12-16 06:55:28 所属栏目:asp.Net 来源:网络整理
导读:我试图访问授权服务器,当用户提供用户名和密码时,该服务器会发出短期访问令牌和更长时间的刷新令牌. 客户端是否应该在每次调用API时都传递刷新令牌以及访问令牌,或者客户端是否只在从API接收到访问令牌已过期的错误代码时才传递刷新令牌? 刷新令牌过期后,将
我试图访问授权服务器,当用户提供用户名和密码时,该服务器会发出短期访问令牌和更长时间的刷新令牌.

>客户端是否应该在每次调用API时都传递刷新令牌以及访问令牌,或者客户端是否只在从API接收到访问令牌已过期的错误代码时才传递刷新令牌?
>刷新令牌过期后,将哪种类型的错误代码传递回客户端?这意味着客户端需要通过再次传递用户名和密码来请求新的访问令牌.

解决方法

问题1:

Should the client pass the refresh token on every call to the API along with the access token or should the client only pass the refresh token once they receive an error code from the API that the access token has expired?

在访问令牌过期之前,客户端不需要刷新令牌.
每个呼叫都需要访问令牌,但只有授予新访问令牌的请求才需要刷新令牌.

要获取新的访问令牌,请将grant_type设置为refresh_token的请求发送,如section 6 of the RFC所示.
理想情况下,在当前访问令牌过期之前,您需要新的访问令牌,以免中断服务.

我见过的大多数实现都会为每个访问令牌发布一个新的刷新令牌.您可以使用任何有效的刷新令牌来获取新的访问令牌.

问题2:

What type of error code is passed back to the client once the refresh token has expired? This would mean the client needs to request a new access token by passing the username and password again.

不幸的是,RFC没有明确定义错误响应;请参阅RFC的7.2节:

If a resource access request fails,the resource server SHOULD inform
the client of the error. While the specifics of such error responses are beyond the scope of this specification,this document establishes a common registry in Section 11.4 for error values to be shared among OAuth token authentication schemes.

因此,确切的响应取决于服务器.它应该由有问题的服务器定义.

如果服务器提供新的刷新令牌,您将需要在当前的刷新令牌到期之前获得新的刷新令牌.

您不想再次发送用户的凭据;你不应该拥有它们,更不用说保留它们了. OAuth 2旨在允许第三方访问用户的受保护资源,而无需查看用户的凭据.

您通常会在password_grant或refresh_token调用中获得带有新访问令牌的新刷新令牌. RFC虽然不保证这一点.如果服务器未提供新的刷新令牌,或者无法依赖新的刷新令牌,则必须要求用户再次登录.请注意,此登录是通过授权服务器完成的,授权服务器不一定是您的应用程序.事实上,它可能不是.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读