简体   繁体   English

使用 Polly 刷新令牌的 RestEase 不起作用

[英]RestEase with Polly refresh token doesn't work

really need some help to understand what I'm doing wrong with my reauthorization policy.真的需要一些帮助来了解我在重新授权政策方面做错了什么。

My policy looks like this:我的政策如下所示:

var policy = Policy
            .HandleResult<HttpResponseMessage>(message => message.StatusCode == HttpStatusCode.Unauthorized)
            .RetryAsync(1, async (result, retryCount) =>
            {
                await _authorizeService.RefreshAccessToken();
                RestEaseClient.Token = _authorizeService.AccessToken;
            });

And IRestEaseClient looks like this: IRESTEaseClient 看起来像这样:

public interface IRestEaseClient
{        
    [Header("X-Token")]
    string Token { get; set; }

    ["root")]
    Task SomeMethod();
    .................
}

When the token expires the breakpoint inside the policy retry function gets hit, I successfully receive the new token, set its value to IRestEaseClient.Token header, but for some reason, the retry request goes with the old header value and I still get 401. The next request after this will go fine, without 401 or hitting the reauthorize policy code. When the token expires the breakpoint inside the policy retry function gets hit, I successfully receive the new token, set its value to IRestEaseClient.Token header, but for some reason, the retry request goes with the old header value and I still get 401.此后的下一个请求将 go 正常,没有 401 或点击重新授权策略代码。

The answer is HttpRequestMessage has already been constructed by RestEase, and RestEase is now out of the picture.答案是 HttpRequestMessage 已经被 RestEase 构造好了,而 RestEase 现在已经不在了。 You can change RestEaseClient.Token all you like, that won't have any effect until you call SomeMethod again which constructs a new HttpRequestMessage.您可以随心所欲地更改 RestEaseClient.Token,直到您再次调用 SomeMethod 构造一个新的 HttpRequestMessage 之前,这不会有任何影响。

Look into this discussion for details https://github.com/canton7/RestEase/discussions/192查看此讨论以获取详细信息https://github.com/canton7/RestEase/discussions/192

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

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