如何刷新令牌服务栈打字稿 [英] how to refresh token servicestack typescript

查看:125
本文介绍了如何刷新令牌服务栈打字稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务栈上说,对于普通客户来说应该是这样,但是对于打字稿来说应该有所不同.有人知道怎么做吗?

On servicestack it says that for regular client it should be like that but for typescript it should be somehow different. Anyone knows how to do it?

var client = new JsonServiceClient(baseUrl);
client.OnAuthenticationRequired = () => {
    client.BearerToken = authClient.Send(new Authenticate()).BearerToken;
};

推荐答案

onAuthenticationRequiredrefreshToken的支持已添加到TypeScript v0.0.32 中的rel ="nofollow noreferrer"> servicestack-client ,可用于透明地处理 401未经授权响应并重新验证从回调中自动重试原始失败请求之前的

Support for onAuthenticationRequired and refreshToken was added to the TypeScript servicestack-client in v0.0.32 where they can be used to transparently handle 401 Unauthorized Responses and re-authenticate the JsonServiceClient from within the callback before it automatically retries the original failed request:

如果服务器返回 401未经授权响应,或者是由于客户端未经身份验证或者所使用的已配置承载令牌或API密钥已过期(或已失效),则可以使用onAuthenticationRequired回调来重新-在自动重试原始请求之前配置客户端,例如:

If the server returns a 401 Unauthorized Response either because the client was Unauthenticated or the configured Bearer Token or API Key used had expired (or was invalidated), you can use onAuthenticationRequired callback to re-configure the client before automatically retrying the original request, e.g:

client.onAuthenticationRequired = async () => {
    const authClient = new JsonServiceClient(authBaseUrl);
    authClient.userName = userName;
    authClient.password = password;
    const response = await authClient.get(new Authenticate());
    client.bearerToken = response.bearerToken;
};

//Automatically retries requests returning 401 Responses with new bearerToken
var response = await client.get(new Secured());

自动刷新JWT令牌

通过 JWT中的刷新令牌支持,您可以使用属性,指示Service Client在由于无效或过期的JWT而自动重试失败的请求之前,在后台自动获取新的JWT令牌,例如:

Automatically refresh JWT Tokens

With the Refresh Token support in JWT you can use the refreshToken property to instruct the Service Client to automatically fetch new JWT Tokens behind-the-scenes before automatically retrying failed requests due to invalid or expired JWTs, e.g:

//Authenticate to get a new Refresh Token
const authClient = new JsonServiceClient(authBaseUrl);
authClient.userName = userName;
authClient.password = password;
const authResponse = await authClient.get(new Authenticate());

//Configure client with RefreshToken
client.refreshToken = authResponse.RefreshToken;

//Clients will automatically retrieve new JWT Tokens as needed
var response = await client.get(new Secured());

将刷新令牌发送到备用服务器

当刷新令牌需要发送到发布新JWT令牌的其他ServiceStack Server时,请使用refreshTokenUri属性,例如:

client.refreshToken = refreshToken;
client.refreshTokenUri = authBaseUrl + "/access-token";

这篇关于如何刷新令牌服务栈打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆