我使用 Duende IdentityServer 7 作为我的身份提供商,并使用 .NET MAUI 作为我的 iOS 应用程序。我已使用 RedirectUri 和 PostLogoutRedirectUri 向身份服务器注册了客户端。登录流程工作正常,但注销流程则不然。在 IdentityServer 中,PostLogoutRedirectUri 变空,因此,它不会重定向到应用程序并停留在浏览器视图中。
我的 .NET MAUI 客户端为
private static void AddService(this IServiceCollection services)
{
services.AddTransient<WebAuthenticatorBrowser>();
services.AddTransient<OidcClient>(sp =>
new OidcClient(new OidcClientOptions
{
Authority = "https://identity-url"
ClientId = "CLientId",
RedirectUri = "myapp://",
PostLogoutRedirectUri = "myapp://",
Scope = "scope1 scope2,
Browser = sp.GetRequiredService<WebAuthenticatorBrowser>(),
DisablePushedAuthorization = false
})
);
}
单击“注销”后,它会调用 OidcClient,如下所示:
LogoutRequest logoutRequest = new LogoutRequest();
LogoutResult? logoutResult = await OidcClient.LogoutAsync(logoutRequest);
我也尝试过这个,但没有任何运气。
LogoutRequest logoutRequest = new LogoutRequest
{
IdTokenHint = token
};
LogoutResult? logoutResult = await OidcClient.LogoutAsync(logoutRequest);
我的身份服务器代码如下所示。
[SecurityHeaders]
[AllowAnonymous]
public class LoggedOut : PageModel
{
private readonly IIdentityServerInteractionService _interactionService;
public LoggedOutViewModel View { get; set; } = new();
public LoggedOut(IIdentityServerInteractionService interactionService)
{
_interactionService = interactionService;
}
public async Task OnGet(string logoutId)
{
LogoutRequest logout = await _interactionService.GetLogoutContextAsync(logoutId);
View = new LoggedOutViewModel
{
AutomaticRedirectAfterSignOut = LogoutOptions.AutomaticRedirectAfterSignOut,
PostLogoutRedirectUri = logout.PostLogoutRedirectUri ?? "",
ClientName = string.IsNullOrEmpty(logout.ClientName) ? logout.ClientId ?? string.Empty : logout.ClientName,
SignOutIframeUrl = logout.SignOutIFrameUrl ?? string.Empty,
};
}
}
对于 MAUI 应用程序,PostLogoutRedirectUri 始终为空。我可以看到 logoutId 被传递给该方法。使用相同身份服务器的其他 WebClient 按预期工作。
知道我在这里做错了什么吗?
问题解决了。我传递的是访问令牌而不是身份令牌。
LogoutRequest logoutRequest = new LogoutRequest
{
IdTokenHint = token // has to be identity token
};
LogoutResult? logoutResult = await OidcClient.LogoutAsync(logoutRequest);