繁体   English   中英

带有keycloak的OpenID,成功登录ASP.NET MVC 4.7后的无限重定向循环

[英]OpenID with keycloak, infinite redirect loop after successful login ASP.NET MVC 4.7

我已经像这样设置了我的 ASP.NET MVC 4.7 应用程序。

除了文件 bello 之外,原始生成的项目没有任何变化。

问题是,我可以成功重定向到我的 Keycloak 登录页面,但是当它重定向到成功登录后指定的 url 时,它会重新路由回身份服务器(即 keycloak),并且身份服务器会重新路由回重新路由的 URL。

这是开发工具日志,看起来确实正确传递了 cookie 和会话

在 Keycloak 页面成功登录后,它重定向到/home ,这是正确的,因为这是我设置的

在此处输入图像描述

看起来 cookie 确实已正确传递:

在此处输入图像描述

在此处输入图像描述

但是,似乎在调用 /home (重定向)之后它在 Keycloak 中再次调用了身份验证

在此处输入图像描述

这导致无限循环。 因为身份验证随后会调用/home ,而 home 会一次又一次地调用身份验证。

我已经尝试过我在互联网上找到的方法,包括使用UseKentorOwinCookieSaver 、使用SystemWebCookieManager ,以及我在网上尝试过但没有成功的任何方法。

我在这里错过了什么? 帮助帮助,我已经在这个问题上停留了好几天了。

这是代码

启动.cs

using Microsoft.Owin;
using Owin;
using System;
using System.Threading.Tasks;

using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Owin.Security.Keycloak;
using Microsoft.Owin.Security.OpenIdConnect;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using System.IdentityModel.Tokens;
using Microsoft.Owin.Host.SystemWeb;

[assembly: OwinStartup(typeof(AspNetMVC4.Startup))]

namespace AspNetMVC4
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseKentorOwinCookieSaver();

            const string persistentAuthType = "keycloak_auth";
            app.SetDefaultSignInAsAuthenticationType(persistentAuthType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = persistentAuthType,
                AuthenticationMode = AuthenticationMode.Active,
                CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebCookieManager()
            });

            var desc = new AuthenticationDescription();
            desc.AuthenticationType = "keycloak_auth";
            desc.Caption = "keycloak_auth";

             app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                AuthenticationType = "Auth0",

                Authority = "http://localhost:8080/auth/realms/master",

                ClientId = "keycloakdemo",
                ClientSecret = "tUM2gZiW5H3Lx2DQ4b5t4x5FzzrmADGi",

                // RedirectUri = "http://localhost:44337/",
                //PostLogoutRedirectUri = auth0PostLogoutRedirectUri,
                RedirectUri = "https://localhost:44337/home",

                ResponseType = OpenIdConnectResponseType.Code,
                Scope = "openid profile email",
                
                CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebCookieManager(),
            });
        }
    }
}

家庭控制器.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AspNetMVC4.Controllers
{
   public class HomeController : Controller
    {
        [Authorize]
        public ActionResult Index()
        {         
            return View();
        }

        public ActionResult About()
        {
            bool flag = User.Identity.IsAuthenticated;
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

您需要确保使用 samesite=none 并使用 HTTPS 来使 cookie 正常工作。

我终于想通了并成功地将 Keycloak 集成到 ASP.NET MVC 4.7 中,我在这里发布我的解决方案以帮助那些将遇到我遇到的相同问题的人。 问题是,Keycloak 和 OWIN/OpenID 并没有无缝集成到 ASP.NET MVC 框架库中,所以我想手动处理所有内容,包括用户身份,处理令牌和身份,并使用令牌检索我需要的信息Keycloak 自己的 Rest API。 我在这里做了一个快速而肮脏的演示:

https://github.com/ruellm/ASPNetMVC4-Keycloak

希望有一天它能帮助一个灵魂,因为我被困了将近 2 周,终于解决了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM