在asp.net core 2.0中使用identityserver4时出现无限身份验证循环

问题描述 投票:0回答:7

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

我的Web应用程序是asp.net core 2.0,它的url是http://localhost:60002。该应用程序将使用 Identity Server 的登录页面。

我希望登录后,Identity Server会重定向到应用程序页面(http://localhost:60002

这是客户端应用程序的Startup.cs

启动.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" });
            });
        }
    }

这是 Identity Server 的登录页面

但是存在一个无限循环,调用http://localhost:9000/connect/authorize端点,然后返回到http://localhost:60002/signin-oidc并显示“错误请求 - 请求”太长了”如下。

当我查看cookie时,有很多项目“.AspNetCore.Correlation.OpenIdConnect.xxx”

这是 Identiy Server 上的日志。表示 Identiy.Application 已成功验证。

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

致以诚挚的问候,

凯文

identityserver4 asp.net-core-2.0
7个回答
22
投票

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

这里的问题是,app.UseAuthentication() 必须在新的 app.UseAuthorization() 之前调用;

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

只有万一有人也遇到这个问题......


6
投票

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

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

然后使用以下代码。

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

4
投票

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

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

删除该部分并重试。


2
投票

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

 public IActionResult SignIn()
        {

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

0
投票

嗯,您的 Identity Server 日志中确实显示了一个很长的请求 - 并且错误显示“错误请求 - 请求太长”。我猜问题是你的要求太大了:) HTTP GET 请求的最大长度?

您是否尝试过发布而不是使用 GET?


0
投票

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


0
投票

我在Azure Portal和IdentityServer4中同时发生了这种情况。 原因:我更改了电脑中的本地日期以测试定期计费,但我忘记了。当我尝试登录 Azure 门户或我的身份服务器时,它进入了无限身份验证循环。

解决方案:本地电脑 -> 控制面板 -> 日期和时间设置 -> 自动设置时间

希望对你有帮助

© www.soinside.com 2019 - 2024. All rights reserved.