繁体   English   中英

尝试使用具有ASP.NET Identity的cookie身份验证登录时获取HTTP 500

[英]Getting HTTP 500 when trying to log in using cookie authentication with ASP.NET Identity

ASP.NET Core 2.2

我已经为这个问题努力了很长时间...

每当我尝试与通过HttpContext登录或退出相关的任何操作时,都会收到HTTP 500错误。

我已经搭建好身份并完成了所需的更改,所以我或多或少地一对一使用了Microsoft的GitHub代码。


我正在执行的操作已完成(登录/注销,更改密码), 但是 RedirectToPage()调用仅返回HTTP 500错误。

我怀疑它必须做一些重新加载太快而不要等待HttpContext登录或认证的事情吗?

我已经设法使用Response.Redirect(returnUrl)而不是RedirectToPage()使登录工作

这是我的Startup.cs

   public void ConfigureServices(IServiceCollection services)
    {
        // Add httpcontext service
        services.AddHttpContextAccessor();
        // Services identity depends on
        services.AddScoped<IIdentityRepository, IdentityRepository>();
        //services.AddOptions().AddLogging();

        // Services used by identity
        services.AddScoped<IUserStore, UserStore>();
        services.AddScoped<IUserValidator, UserValidator>();
        services.AddScoped<IPasswordValidator, PasswordValidator>();
        services.AddScoped<IPasswordHasher, PasswordHasher>();
        services.AddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
        // No interface for the error describer so we can add errors without rev'ing the interface
        services.AddTransient<IdentityErrorDescriber>();
        services.AddScoped<UserManager>();
        services.AddScoped<SignInManager>();

        // Identity cookie paths
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(o =>
            {
                o.AccessDeniedPath = "/Identity/Account/AccessDenied";
                o.LoginPath = "/Identity/Account/Login";
                o.Cookie.HttpOnly = true;
            });


        // Require authorization on every page by default
        // Allow areas and add an area to path
        services.AddMvc(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddRazorPagesOptions(options =>
            {
                options.AllowAreas = true;
                options.Conventions.AddAreaPageRoute("App", "/summary", "");
            });

        services.AddRouting(options => options.LowercaseUrls = true);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();
        app.UseAuthentication();
        app.UseMvc();
    }

同样用于更改密码的剃须刀页面示例:(正确输入正确的旧密码和新密码后,我收到HTTP 500错误)

public async Task<IActionResult> OnPostAsync () {
if (!ModelState.IsValid) {
    return Page ();
}

var user = await _userManager.GetUserAsync (User);
if (user == null) {
    return NotFound ($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}

var changePasswordResult = await _userManager.ChangePasswordAsync (user, Input.OldPassword, Input.NewPassword);
if (!changePasswordResult.Succeeded) {
    foreach (var error in changePasswordResult.Errors) {
        ModelState.AddModelError (string.Empty, error.Description);
    }
    return Page ();
}

await _signInManager.RefreshSignInAsync (user);
//_logger.LogInformation("User changed their password successfully.");
StatusMessage = "Your password has been changed.";

return RedirectToPage(); // THIS PROBABLY CAUSING THE HTTP 500

}

感谢@poke我得到了答案。

当您调试应用程序时,在浏览器中出现500错误时未显示的异常将显示在Visual Studios“输出”窗口中。

我的特定问题是在User Store类中尚未实现的Dispose方法抛出。

暂无
暂无

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

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