如何解决无法确定配置为子目录的 Web 表单应用程序上 IdentityServer4 OpenID Connect 身份验证的 RedirectUri
我知道有很多与我类似的问题,但我找不到任何示例,其中客户端应用程序的配置与我的设置一样。
我想做什么
我已经使用 Asp.Net Identity 和 EntityFramework 核心配置了一个 asp.net 核心 3.1 IdentityServer4 实例。我正在尝试从 FormsAuthentication 迁移一堆 Web 表单应用程序,并且我的配置基本正确 - 我可以解决我的登录重定向问题,并看到我的声明按预期返回并且我的角色正在工作。
我的问题
但是,我就是无法找出正确的 RedirectUri,这可能至少部分是因为我不了解 signin-oidc 端点的工作原理以及是否可以对其进行配置。
在我见过的几乎所有示例中,重定向 Uri 始终指向根应用程序(对于 localhost 开发,它类似于 localhost:5001 或应用程序正在侦听的任何端口)。
我的 IIS 主机设置
对于本地开发和生产,我的应用程序在 IIS 中设置为一个根应用程序,并以一系列其他应用程序作为子目录。例如:
- mydomain.com
- mydomain.com/Application1
- mydomain.com/Application2
每一个都是一个独立的应用程序。
我尝试过的重定向 Uris
我看到的示例将重定向 Uri 显示为 localhost:5001/signin-oidc。以下是我在 Startup.cs 文件中尝试过的各种重定向 Uris 及其结果:
- localhost/Application1:来自 Firefox 的“此页面未正确重定向”消息(url 类似于 http://localhost:80/Application1?code=xxxx&scope=xxxx&state=xxxx&session_state=xxxx)
- localhost/Application1/:重定向到 root (localhost)。如果我之后手动导航到该应用程序,我可以访问该应用程序,并且一切正常。但是,这是一个站不住脚的解决方案,因为我的用户会感到非常困惑。
- localhost/Application1/signin-oidc: 404 not found (url 类似于:http://localhost:80/Application1/signin-oidc?code=xxxx&scope=xxxx&state=xxxx&session_state=xxxx)
- localhost/Application1/signin-oidc/:与 localhost/Application1/ 相同:重定向到 root
因此,如果我的应用程序位于服务器的根目录下,这似乎一切正常,但由于它不是,我不知道如何使重定向正常工作。
参考
这里参考的是我的应用程序的 startup.cs 中的代码和产生挑战的代码。就其价值而言,我不明白为什么在身份验证质询中指定了 RedirectUri 或它对请求有什么影响,但除非我将其设置为此处,否则它似乎不起作用,并且在所有示例中我看到。我尝试了它,但它没有产生任何不同的结果,与我保持原样并使用 Startup.cs 文件中的 RedirectUri 值相比:
Startup.cs
public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
private void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationType = CookieAuthenticationDefaults.AuthenticationType
});
app.UseOpenIdConnectAuthentication(GetOpenIdConnectAuthenticationOptions());
}
private OpenIdConnectAuthenticationOptions GetOpenIdConnectAuthenticationOptions()
{
var _authority = "http://localhost:5000/";
var _redirectUri = "http://localhost/Application1/";
var _clientId = "app1";
var _clientSecret = "WouldntYouLikeToKnow";
return new OpenIdConnectAuthenticationOptions
{
Authority = _authority,RedirectUri = _redirectUri,ClientId = _clientId,ClientSecret = _clientSecret,Scope = $"{OpenIdConnectScope.OpenIdProfile} role",TokenValidationParameters = new TokenValidationParameters()
{
NameClaimType = "preferred_username",RoleClaimType = ClaimTypes.Role
},SignInAsAuthenticationType = CookieAuthenticationDefaults.AuthenticationType,RequireHttpsMetadata = _authority.Contains("https"),UseTokenLifetime = false,RedeemCode = true,SaveTokens = true,ResponseType = OpenIdConnectResponseType.Code,ResponseMode = OpenIdConnectResponseMode.Query,Notifications = new OpenIdConnectAuthenticationNotifications
{
//section removed for brevity - this code handles pkce verification and challenge.
}
};
}
}
SiteMaster.cs
protected override void OnInit(EventArgs e)
{
if (!Request.IsAuthenticated)
{
HttpContext.Current.GetOwinContext().Authentication.Challenge(new Microsoft.Owin.Security.AuthenticationProperties()
{
RedirectUri = "/"
},OpenIdConnectAuthenticationDefaults.AuthenticationType);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。