首页 > 解决方案 > 当我更新安全标记时,网页永远不会结束加载

问题描述

正如标题所说,每当我使用以下方法更新安全标记时,除了当前会话(更改密码的会话工作正常)之外的任何会话都无法再次进入网页,除非他们手动删除 cookie,我的目标是为了能够撤销到身份服务器的所有 cookie 会话,所以我添加了 SecurityStampValidatorOptions 。我认为这不是 [Authorize] 属性的具体问题,因为即使加载 .well-known/configuration 端点它也会卡住。我不确定我做错了什么。

    [HttpPost]
    [Authorize]
    public async Task<IActionResult> ChangePass(ChangePassViewModel model)
    {
        if (!ModelState.IsValid)
            return View(model);

        var user = await _userManager.GetUserAsync(this.User);

        var changePassword = await _userManager.ChangePasswordAsync(user, model.ViejaPass, model.NuevaPass);

        if (!changePassword.Succeeded)
        {
            foreach (var error in changePassword.Errors)
            {
                ModelState.TryAddModelError(error.Code, error.Description);
            }

            return View(model);
        }

        await _userManager.UpdateSecurityStampAsync(user);

        await _signInManager.SignInAsync(user, true);

        if (string.IsNullOrEmpty(model.RedirectUrl))
            return Redirect("http://localhost:3000"); // aca tenemos que definir a donde lo mandamos
        else
            return Redirect(model.RedirectUrl);

    }

这是我的服务管道

    public void ConfigureServices(IServiceCollection services)
    {
        string conn = ConfigurationExtensions.GetConnectionString(_config, "Conn");
        var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
        //var certificate = CertHelper.GetCertificate(); TODO: look how to import cert to docker

        services.AddRazorPages().AddRazorRuntimeCompilation();

        services.AddDbContext<IdenServerDbContext>(config =>
        {
            config.UseSqlServer(
                conn,
                o => o.MigrationsAssembly(migrationsAssembly)
            );
        });

        services.AddIdentity<User, IdentityRole>(config =>
        {
            config.Password.RequireDigit = false;
            config.Password.RequiredLength = 4;
            config.Password.RequireUppercase = false;
            config.Password.RequireNonAlphanumeric = false;

            config.SignIn.RequireConfirmedEmail = true;
        })
        .AddEntityFrameworkStores<IdenServerDbContext>()
        .AddDefaultTokenProviders();

        services.ConfigureApplicationCookie(config =>
        {
            config.Cookie.Name = "IdentityServer.Cookie";
            config.LoginPath = "/Auth/Login";
            config.LogoutPath = "/Auth/Logout";
        });

        services.AddOptions();

        services.Configure<SecurityStampValidatorOptions>(options =>
        {
            options.ValidationInterval = TimeSpan.FromMinutes(1);
        });

        services.AddIdentityServer()
                .AddAspNetIdentity<User>()
                .AddConfigurationStore(options =>
                {
                    options.ConfigureDbContext = b => b.UseSqlServer(conn,
                        sql => sql.MigrationsAssembly(migrationsAssembly));
                })
                .AddOperationalStore(options =>
                {
                    options.ConfigureDbContext = b => b.UseSqlServer(conn,
                        sql => sql.MigrationsAssembly(migrationsAssembly));
                })
                .AddDeveloperSigningCredential();

        services.AddTransient<CustomUserManager>();
        services.AddScoped<IEmpresaService, EmpresaService>();
        services.AddScoped<IImagenService, ImagenService>();


        services.AddMvc(options => {
            options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
        });
    }

这是我的配置管道

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, CustomUserManager userManager, RoleManager<IdentityRole> roleManager)
    {
        //No es ideal dejar esto aca por lo cual se recomienda sacarlo una vez populada la base de datos
        app.InitializeDatabaseIdentity();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        if (_env.IsDevelopment())
        {
            app.UseCookiePolicy(new CookiePolicyOptions()
            {
                MinimumSameSitePolicy = SameSiteMode.Lax
            });
        }

        app.UseStaticFiles();

        app.UseNodeModules(env.ContentRootPath);

        app.UseDistFolder(env.ContentRootPath);

        app.UseRouting();

        app.UseIdentityServer();

        app.UseAuthorization();


        app.UseEndpoints(endpoints =>
        {
            endpoints.MapDefaultControllerRoute();
        });

        //esto esta aca solo para las pruebas jamas deberia existir
        app.InitializeDatabaseMock(userManager, roleManager);
    }

编辑:

1-当我说我无法进入网页时,我的意思是来自 idp 服务器的任何端点,不是由身份自动处理的部分,也不是我自己的 mvc 处理登录和重定向的部分,它只是永远不会得到服务器的响应它永远在等待。在服务器端,无论我在哪里放置断点,都没有达到断点。但是使用新登录或更改密码的登录一切正常。

2-找到第 2 号,我开始了一个新项目,只有 aspnet 身份而没有身份服务器 4,并且 cookie 可以正常工作,正如预期的那样。我稍后会编辑,但我会在管道中添加更多内容,直到我看到失败的地方

标签: c#asp.net-coreidentityserver4

解决方案


推荐阅读