c# - 验证 MVC 应用程序会导致 OpenID Connect 3 的无限重定向循环
问题描述
问题
我在使用OpenID Connect 3为我正在开发的网站提供授权时遇到问题。
问题是这样的:
- 我访问了一个受保护的页面并被重定向到 IdentityServer (OpenID Connect 3)
- 身份服务器询问我的用户名和密码
- 这些凭据被认为是好的,然后我被转发回 MVC 站点。
- 这就是出错的地方。由于某种原因,该站点仍然未经身份验证,并且在那里重定向回 Identityserver。
- 就身份服务器而言,我已经登录,所以它会将我重定向回 mvc 应用程序
第 4 步和第 5 步将永远进行......好吧,除了达到最大 cookie 数量的限制外,它们会结束一切。
尝试过的解决方案
经过几天的谷歌搜索,我尝试了以下方法,但到目前为止,没有什么对我有用。
Kentor Owin Cookie Fix中的 Startup 类.. ConfigureAuth 功能
app.UseKentorOwinCookieSaver();
1的变体
app.UseKentorOwinCookieSaver(PipelineStage.Authenticate);
替代 cookie 管理器SystemWebCookieManager
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "Cookies", CookieManager = new SystemWebCookieManager() });
3 SystemWebChunkingCookieManager的变体
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "Cookies", CookieManager = new SystemWebChunkingCookieManager() });
Global.asa 文件中的会话存根
protected void Session_Start() { } protected void Session_End() { }
我确实尝试了其他一些其他开发人员在那里编写自己的代码来尝试和修复的东西。我有点疯狂,因为似乎没有任何效果。有没有其他人来过这里,可以给我一个关于下一步我能做什么的线索。下面是来自我的 OpenIdServer 和 MVC 应用程序的相关代码。
身份服务器配置
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IDServer.Config;
using IdentityServer3.Core.Configuration;
using System.Security.Cryptography.X509Certificates;
[assembly: OwinStartup(typeof(IDServer.Startup))]
namespace IDServer
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Map("", idsrvApp =>
{
idsrvApp.UseIdentityServer(new IdentityServer3.Core.Configuration.IdentityServerOptions
{
SiteName = "Identity Server",
IssuerUri = "https://localhost:44398/embedded",
Factory = new IdentityServerServiceFactory()
.UseInMemoryClients(Clients.Get())
.UseInMemoryScopes(Scopes.Get())
.UseInMemoryUsers(Users.Get()),
SigningCertificate = LoadCertificate(),
RequireSsl = true,
});
});
}
X509Certificate2 LoadCertificate()
{
return new X509Certificate2(
string.Format(@"{0}\Certificates\idsrv3test.pfx",
AppDomain.CurrentDomain.BaseDirectory), "idsrv3test");
}
}
}
服务器上的客户端注册配置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IdentityServer3.Core.Models;
namespace IDServer.Config
{
public static class Clients
{
public static IEnumerable<Client> Get()
{
return new[]
{
new Client
{
Enabled=true,
ClientName = "My Application",
ClientId = "MyApp",
Flow = Flows.Hybrid,
//Flow=Flows.Implicit,
RequireConsent = true,
RedirectUris = new List<string> { "https://localhost:44362/" },
AllowedScopes = new List<string> {"openid"}
}
};
}
}
}
网络应用配置
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;
using System;
using System.Diagnostics;
using System.Web;
using Microsoft.Owin.Host.SystemWeb;
using Microsoft.Owin.Infrastructure;
[assembly: OwinStartup(typeof(MyApp.Startup))]
namespace MyApp {
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.UseKentorOwinCookieSaver();
//app.UseKentorOwinCookieSaver(PipelineStage.Authenticate);
app.SetDefaultSignInAsAuthenticationTypeCookieAuthenticationDefaults
.AuthenticationType);
ICookieManager c = new SystemWebCookieManager();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
//CookieManager = new SystemWebChunkingCookieManager()
//CookieManager = new SystemWebCookieManager()
CookieManager = c
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = "MyApp",
Authority = "https://localhost:44398/",
RedirectUri = "https://localhost:44362/",
SignInAsAuthenticationType = "Cookies",
ResponseType = "code id_token",
Scope = "openid",
RequireHttpsMetadata = true,
CallbackPath = new PathString("/home/contact/"),
Notifications = new OpenIdConnectAuthenticationNotifications()
{
MessageReceived = async n =>
{
Debug.Print(n.ProtocolMessage.IdToken);
}
}
}
}
解决方案
我终于设法通过将 AutheticationType 设置为 Active 来使其工作。请参阅下面的片段。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
AuthenticationMode = AuthenticationMode.Active
});
推荐阅读
- react-native - React Native StatusBar:黑暗内容不适用于android
- python - 将日期 (YYYY-MM-DD) 转换为一年中的某一天 (0-364)
- c# - StartCoroutine 用于延迟反应
- algorithm - 寻找图连通性的算法
- java - 带有红色下划线的 Maven 插件 + 在类路径中定义名称为“entityManagerFactory”的 bean 创建错误
- c++ - lua 5.0.2 模块和 5.3.5 有什么区别?
- google-sheets - 合并列而不删除或影响行
- spring-boot - 在 Spring Boot 测试中使用 UriComponentsBuilder/MvcComponentsBuilder
- html - 使用 nth-of-type 对齐图像
- flutter - 检查 SQFLITE 数据库中存在 PK 列表中的哪些 PK