我正在使用带有IdentityServer4的新的独立Blazor WASM身份验证流。我想向用户提供一条消息,说明他们由于不活动而注销。我已经用低质量的js alert((实现了这一功能,但我想知道我是否可以用自定义弹出窗口或发送到identityserver的重定向参数来在identityserver登录页面上向他们显示警报。
我想不出一种方法来中断OnLogoutSuccessed之后发生的立即重定向。js alert((暂停重定向并工作。我可以修改传出登录重定向uri,为IDS4提供一个参数吗?
<RemoteAuthenticatorView Action="@Action" OnLogOutSucceeded="LogoutSucceeded">
</RemoteAuthenticatorView>
@code{
[Parameter] public string Action { get; set; }
private async Task LogoutSucceeded()
{
await JsInterop.InvokeVoidAsync("alert", "You have been logged out due to inactivity.");
}
}
我发现了:
//program.cs
builder.Services.AddOidcAuthentication<ApplicationAuthenticationState>(options =>
{
//options
});
//Authentication.razor
<RemoteAuthenticatorViewCore Action="@Action"
TAuthenticationState="ApplicationAuthenticationState"
OnLogOutSucceeded="LogoutSucceeded"
AuthenticationState="AuthState" />
[Parameter]
public string Action { get; set; }
public ApplicationAuthenticationState AuthState { get; set; } = new ApplicationAuthenticationState();
public bool Idled { get; set; }
protected override void OnInitialized()
{
if (RemoteAuthenticationActions.IsAction(RemoteAuthenticationActions.LogOut, Action))
{
var uri = NavManager.ToAbsoluteUri(NavManager.Uri);
if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("idle", out var param))
{
AuthState.Idle = !string.IsNullOrWhiteSpace(param);
}
}
}
private void LogoutSucceeded(ApplicationAuthenticationState state)
{
Idled = state.Idle;
if (Idled)
{
// save redirect for later
var returnUrl = state.ReturnUrl;
// cancel redirect
state.ReturnUrl = null;
// implement custom flow
}
}