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

尝试使用具有ASP.NET标识的cookie身份验证登录时获取HTTP 500,asp.net,asp.net-core,asp.net-identity,cookie-authentication,Asp.net,Asp.net Core,Asp.net Identity,Cookie Authentication,ASP.NET核心2.2 我和这个问题斗争了很长时间 每当我试图通过HttpContext登录或注销任何相关内容时,我都会收到一个HTTP 500错误 我已经构建了身份并做了所需的更改,所以我或多或少地使用了microsofts GitHub代码一对一 我正在执行的操作已经完成(登录/注销、更改密码)但是RedirectToPage()调用只返回一个HTTP 500错误 我怀疑这与重新加载太快以及不等待HttpContext登录或验证有关 我通过使用Response.Redirect(retu

ASP.NET核心2.2

我和这个问题斗争了很长时间

每当我试图通过HttpContext登录或注销任何相关内容时,我都会收到一个HTTP 500错误

我已经构建了身份并做了所需的更改,所以我或多或少地使用了microsofts GitHub代码一对一


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

我怀疑这与重新加载太快以及不等待HttpContext登录或验证有关

我通过使用
Response.Redirect(returnUrl)

这是我的
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();
    }
public void配置服务(IServiceCollection服务)
{
//添加httpcontext服务
AddHttpContextAccessor();
//服务标识取决于
services.addScope();
//services.AddOptions().AddLogging();
//身份使用的服务
services.addScope();
services.addScope();
services.addScope();
services.addScope();
services.addScope();
//没有用于错误描述符的接口,因此我们可以添加错误,而无需修改接口
services.AddTransient();
services.addScope();
services.addScope();
//标识cookie路径
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(o=>
{
o、 AccessDeniedPath=“/Identity/Account/AccessDenied”;
o、 LoginPath=“/Identity/Account/Login”;
o、 Cookie.HttpOnly=true;
});
//默认情况下,在每个页面上都需要授权
//允许区域并将区域添加到路径
services.AddMvc(选项=>
{
var policy=new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()文件
.Build();
options.Filters.Add(新的授权过滤器(策略));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddRazorPagesOptions(选项=>
{
options.AllowAreas=true;
options.Conventions.AddAreaPageRoute(“App”、“/summary”、”);
});
services.AddRouting(options=>options.LowercaseUrls=true);
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc();
}
用于更改密码的razor页面示例: (正确输入正确的旧密码和新密码后,我得到一个HTTP 500错误)

公共异步任务OnPostAsync(){ 如果(!ModelState.IsValid){ 返回页(); } var user=await\u userManager.GetUserAsync(用户); if(user==null){ 返回NotFound($“无法加载ID为“{{u userManager.GetUserId(user)}”的用户”); } var changePasswordResult=wait _userManager.ChangePasswordAsync(user,Input.OldPassword,Input.NewPassword); 如果(!changePasswordResult.successed){ foreach(changePasswordResult.Errors中的var错误){ ModelState.addmodeleror(string.Empty,error.Description); } 返回页(); } wait _signInManager.RefreshSignInAsync(用户); //_logger.LogInformation(“用户成功更改了密码”); StatusMessage=“您的密码已更改。”; return RedirectToPage();//这可能导致HTTP 500 }多亏了你,我才得到了答案

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


我的具体问题是在用户存储类中尚未实现的Dispose方法抛出

能否检查ASP.NET核心服务器日志?错误500通常意味着服务器端存在未处理的异常,框架应自动记录该异常。因此,通过查看日志,您应该能够获得更多关于那里到底出了什么问题的信息。IISExpress只是记录请求,没有其他内容,并且.Net Core本身应该显示任何错误,因为启用了UsedeveloperCeptionPage()。如果您使用IISExpress通过Visual Studio运行应用程序,当您在下拉列表中选择“ASP.NET核心服务器”时,您应该可以在输出面板中看到实际的ASP.NET核心服务器日志。@谢谢!我从来不知道它打印了IISExpress的错误。发现了我的问题,一切都正常了。在本例中,问题是UserManager类中的Dispose方法引发了一个尚未实现的错误。奇怪的是,它不会显示错误消息?知道是什么原因吗?当您在调试下拉选项下调试时,它也会显示日志。下拉列表中没有“ASP.NET Core server”。它既不是IISExpress错误,也不是IISExpress日志。这是ASP.NET核心应用程序日志。IISExpress(和ISS)没有做太多的工作,只是将请求转发到运行ASP.NET核心应用程序的Kestrel服务器。这就是为什么应用程序中有一个单独的日志。如果直接运行应用程序(不使用IISExpress),默认情况下还将在控制台中看到日志输出。及
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