刷新的令牌Blazor Server端OpenID Connect
我正在努力在Blazor Server端应用程序中获得身份验证,以按预期工作。
我一直在遵循此文档,并添加了注册范围的服务: asp.net Core Blazor Server其他安全方案
来自_HOST.CSHTML
我正在从httpcontext获取令牌:
var tokens = new InitialApplicationState
{
AccessToken = await HttpContext.GetTokenAsync("access_token"),
RefreshToken = await HttpContext.GetTokenAsync("refresh_token"),
IdToken = await HttpContext.GetTokenAsync("id_token"),
ExpiresAtUtc = expiryTime.UtcDateTime,
};
然后将其传递给>
>
>
> App.Razor
。
protected override Task OnInitializedAsync()
{
var shouldSetInitialValues = TokenProvider.AccessToken is null || TokenProvider.RefreshToken is null;
if (shouldSetInitialValues)
{
TokenProvider.AccessToken = InitialState!.AccessToken;
TokenProvider.RefreshToken = InitialState.RefreshToken;
TokenProvider.IdToken = InitialState.IdToken;
TokenProvider.ExpiresAtUtc = InitialState.ExpiresAtUtc;
}
return base.OnInitializedAsync();
}
我遇到的问题是,有时HTTPContext中的访问已到期。发生这种情况时,我只想刷新令牌。我有适当的代码,以确保在存在访问时,令牌是有效的。
但是,每次发送新请求,或者在导航到另一个页面时,tokenProvider
都会被清除,因此shordSetSetInitialValues
始终设置为true。 然后,始终将过期的访问权传递到TokenProvider
中。
一旦过期,如何更新访问权限?
I'm having struggling to get the authentication in a Blazor server side app to work as expected.
I have been following this documentation, and added registered the scoped service: ASP.NET Core Blazor Server additional security scenarios
From _Host.cshtml
I am fetching the tokens from HttpContext:
var tokens = new InitialApplicationState
{
AccessToken = await HttpContext.GetTokenAsync("access_token"),
RefreshToken = await HttpContext.GetTokenAsync("refresh_token"),
IdToken = await HttpContext.GetTokenAsync("id_token"),
ExpiresAtUtc = expiryTime.UtcDateTime,
};
They are then passed to App.razor
.
protected override Task OnInitializedAsync()
{
var shouldSetInitialValues = TokenProvider.AccessToken is null || TokenProvider.RefreshToken is null;
if (shouldSetInitialValues)
{
TokenProvider.AccessToken = InitialState!.AccessToken;
TokenProvider.RefreshToken = InitialState.RefreshToken;
TokenProvider.IdToken = InitialState.IdToken;
TokenProvider.ExpiresAtUtc = InitialState.ExpiresAtUtc;
}
return base.OnInitializedAsync();
}
The problem I am having, is that sometimes the AccessToken in the HttpContext has expired. When this happens, I simply want to refresh the token. I have code in place that makes sure that the token is valid when an AccessToken is present.
But every time a new request is sent, or when navigating to a different page, the TokenProvider
is cleared, so shouldSetInitialValues
is always set to true.
Then a expired AccessToken is always passed into the TokenProvider
.
How do I update the AccessToken once expired?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过更新存储在httpcontext中的令牌来解决此问题。
首先,我试图使用新创建的剃须刀页面来执行此操作,但遇到了一些问题,以后将用户重定向。
最终,我放置了直接在_host.cshtml中更新令牌的逻辑。
编辑:
_host.cshtml的内部我检查令牌是否已过期:
方法refreshaccessTokenAsync()看起来像这样:
希望这会有所帮助!
I solved this by updating the tokens stored in the HttpContext instead.
First I was trying to do this with a newly created razor page, but ran into some issues with redirecting the user afterwards.
Eventually I placed the logic for updating the tokens directly in the _Host.cshtml.
EDIT:
Inside of _Host.cshtml I check if the token has expired:
The method RefreshAccessTokenAsync() looks something like this:
Hope this helps!
以下中间软件可用于更新令牌。
这就是我配置身份验证的方式:
The following middlerware can be used to renew the token.
This is how I configured my authentication: