在 asp.net 核心 2.0 中使用 identityserver4 时的无限身份验证循环

Infinite authentication loop when using identityserver4 in asp.net core 2.0

我有一个使用 identityserver4 框架的身份服务器,它的 url 是 http://localhost:9000

我的网络应用程序是 asp.net 核心 2.0,它的 url 是 http://localhost:60002。此应用程序将使用 Identity Server 的登录页面。

我想在登录后,身份服务器将重定向到应用程序页面(http://localhost:60002)

这里是客户端应用Startup.cs

Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        private string AuthorityUri => Configuration.GetValue<string>("UserManagement-Authority");

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();            

            services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(options =>
            {
                options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.Authority = AuthorityUri; // "http://localhost:9000"
                options.RequireHttpsMetadata = false;
                options.ClientId = "customer.api";
                options.ClientSecret = "testsecret";
                options.ResponseType = "code id_token";
                options.Scope.Add("customerprivatelinesvn.api");
                options.Scope.Add("offline_access");
                options.GetClaimsFromUserInfoEndpoint = true;
                options.SaveTokens = true;
            });

            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
                {
                    HotModuleReplacement = true
                });
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();            

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller = "Home", action = "Index" });
            });
        }
    }

这是身份服务器上的登录页面

但是有一个无限循环调用 http://localhost:9000/connect/authorize endpoint, and then it returns to http://localhost:60002/signin-oidc 和 "Bad Request - Request Too Long" 如下。

当我看饼干时,有很多项目 ".AspNetCore.Correlation.OpenIdConnect.xxx"

这是身份服务器上的日志。表示Identiy.Application认证成功

有谁知道这个问题是什么?以及如何解决这个问题?非常感谢。

此致,

凯文

好吧,您的 Identity Server 日志中确实显示了一个很长的请求 - 错误显示 "Bad Request - request too long"。我猜问题是你的要求太大了:)

您试过发帖而不是使用 GET 吗?

在您的客户端应用中,在“启动”中检查您是否有

services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

删除该部分并重试。

从现有的 .NET Core 2.2 项目复制启动代码并将其重新用于新的 .NET Core 3.1 项目后,我也遇到了登录循环。

这里的问题是,必须在新的 app.UseAuthorization();

之前调用 app.UseAuthentication()

https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#migrate-startupconfigure

只有在有人 运行 也关注这个问题的情况下...

就我而言,在从客户端启动登录时,我缺少 RedirectUri。通过如下添加 RedirectUri 解决了问题。

 public IActionResult SignIn()
        {

            return Challenge(new AuthenticationProperties() { RedirectUri = "/" }, "oidc" );
        }

我更新最新的IdentityServer4和.NET Core的nuget包后问题解决了

在客户端应用程序中添加默认身份会导致无限重定向循环。

在客户端应用中,如果需要使用UserManager,RoleManager。

然后使用下面的代码。

services.AddIdentityCore<IdentityUser>()
                .AddRoles<IdentityRole>()
                .AddRoleManager<RoleManager<IdentityRole>>()
                .AddSignInManager<SignInManager<IdentityUser>>()
                .AddEntityFrameworkStores<ApplicationDbContext>();