c# - Kestrel 上的身份验证失败,但 IIS Express 上没有

标签 c# asp.net-core

我是 .net core 的新手,正在尝试使用 asp .net-core 中提供的身份验证功能使用带有登录页面的 Web 应用程序。

当我创建并构建 Web 应用程序时,我使用 IISExpress 运行它,身份验证功能正常工作,允许我登录并在 Web 应用程序上使用各种操作。

我现在正尝试从 IIExpress 更改为 Kestrel,但在登录时对用户进行身份验证时遇到了一些困难。

info: RestartTool.Controllers.AccountController[0]
  User logged in.
info: Microsoft.AspNetCore.Mvc.RedirectToActionResult[1]
  Executing RedirectResult, redirecting to /.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
  Executed action RestartTool.Controllers.AccountController.Login (RestartTool) in 3330.8233ms

因此,当使用 Kestrel 时,用户会正确“登录”,因为输入的用户名/密码是正确的。因此它意味着重定向到索引,或/。

  Request starting HTTP/1.1 GET http://localhost:59211/
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
  Authorization failed for user: (null).
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
  Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
  Executing ChallengeResult with authentication schemes ().

但是在控制台出现了上述错误信息,页面最终在用户未登录的情况下重定向回登录页面。

在我的启动 Configure 方法中,我添加了以下行,很多答案似乎显示了修复,但它没有什么区别(因为它已经存在)

            app.UseAuthentication();

如果它有用,在我的 ConfigureServices 方法中我设置如下:(还有一点点指定密码设置)

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

我很好奇这个问题可能是什么,因为它在 IIExpress 上可以正常工作,但在 Kestrel 上不能。如果您需要我提供更多代码,请告诉我。对此问题的任何帮助将不胜感激。谢谢。

编辑 - ConfigureServices 中的 Cookie 配置:

/* services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.HttpOnly = true;
                options.Cookie.Expiration = TimeSpan.FromDays(150);
                options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
                options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
                options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
                options.SlidingExpiration = true;
            });*/

我基本上是按照本教程来创建它的,我不认为它有很多变化:Introduction to Identity on ASP.NET Core

最佳答案

我通过在 II Express 页面上注销解决了这个问题,然后它能够​​让我登录并在 Kestrel 上成功运行验证。

关于c# - Kestrel 上的身份验证失败,但 IIS Express 上没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48832155/

相关文章:

c# - 未指定 Razor SDK 任务的装配位置

asp.net-core - NLog 初始发送和多次批量发送电子邮件

c# - c# 多个窗体之间的通信

asp.net-core - 发布 polymer + ASP.NET Core

c# - 使用左连接 lambda 的 Entity Framework 加载数据

c# - 具有 Azure 辅助角色的 Ninject

c# - C# 新手,在使用 C# 枚举和类型转换进行计算时遇到问题

c# - XmlNode 值与 InnerText

c# - 使用 datetime.addhours(-1) 时更新日期时间对象的日期

c# - MVC 模型绑定(bind) - 验证每个属性?