如何刷新令牌servicestack打字稿

时间:2017-03-29 13:46:28

标签: servicestack jwt

在servicestack上,它说对于普通客户端应该是这样的,但对于打字稿,它应该有点不同。谁知道怎么做?

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

1 个答案:

答案 0 :(得分:2)

v0.0.32 中的TypeScript servicestack-client添加了对onAuthenticationRequiredrefreshToken的支持,可用于透明地处理 401 Unauthorized 在回调中自动重试原始失败请求之前,回复并重新验证JsonServiceClient中的onAuthenticationRequired

透明处理401 Unauthorized Responses

如果服务器返回 401 Unauthorized 响应,或者因为客户端未经身份验证,或者配置的Bearer Token或API Key已过期(或已失效),则可以使用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()); 回调在自动重试原始请求之前重新配置客户端,例如:

refreshToken

自动刷新JWT标记

使用Refresh Token support in JWT,您可以使用//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令牌,然后由于无效或过期的JWT而自动重试失败的请求,例如:

refreshTokenUri

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

当需要将刷新令牌发送到发布新JWT令牌的其他ServiceStack服务器时,使用client.refreshToken = refreshToken; client.refreshTokenUri = authBaseUrl + "/access-token"; 属性,例如:

VCS