首页 > 解决方案 > 验证 MVC 应用程序会导致 OpenID Connect 3 的无限重定向循环

问题描述

问题

我在使用OpenID Connect 3为我正在开发的网站提供授权时遇到问题。

问题是这样的:

  1. 我访问了一个受保护的页面并被重定向到 IdentityServer (OpenID Connect 3)
  2. 身份服务器询问我的用户名和密码
  3. 这些凭据被认为是好的,然后我被转发回 MVC 站点。
  4. 这就是出错的地方。由于某种原因,该站点仍然未经身份验证,并且在那里重定向回 Identityserver。
  5. 就身份服务器而言,我已经登录,所以它会将我重定向回 mvc 应用程序

第 4 步和第 5 步将永远进行......好吧,除了达到最大 cookie 数量的限制外,它们会结束一切。

尝试过的解决方案

经过几天的谷歌搜索,我尝试了以下方法,但到目前为止,没有什么对我有用。

  1. Kentor Owin Cookie Fix中的 Startup 类.. ConfigureAuth 功能

    app.UseKentorOwinCookieSaver();
    
  2. 1的变体

    app.UseKentorOwinCookieSaver(PipelineStage.Authenticate);
    
  3. 替代 cookie 管理器SystemWebCookieManager

           app.UseCookieAuthentication(new CookieAuthenticationOptions
           {
                AuthenticationType = "Cookies",
               CookieManager = new SystemWebCookieManager()
    
           });
    
  4. 3 SystemWebChunkingCookieManager的变体

           app.UseCookieAuthentication(new CookieAuthenticationOptions
           {
                AuthenticationType = "Cookies",
                CookieManager = new SystemWebChunkingCookieManager()
    
            });
    
  5. 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);
                     }

                 }

         }
     }

标签: c#asp.net-mvcauthenticationinfinite-loopopenid-connect

解决方案


我终于设法通过将 AutheticationType 设置为 Active 来使其工作。请参阅下面的片段。

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = "Cookies",
    AuthenticationMode = AuthenticationMode.Active            
});

推荐阅读