我是 .net core 的新手,正在尝试一个带有登录页面的 web 应用程序,该页面使用 asp .net-core 中提供的身份验证功能。
当我创建并构建 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
Run Code Online (Sandbox Code Playgroud)
因此,在使用 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 ().
Run Code Online (Sandbox Code Playgroud)
但是,上面的错误信息出现在控制台中,页面最终在用户未登录的情况下重定向回登录页面。
在我的启动配置方法中,我添加了以下行,很多答案似乎都显示了修复程序,但它没有区别(因为它已经存在)
app.UseAuthentication();
Run Code Online (Sandbox Code Playgroud)
如果有用,在我的 ConfigureServices 方法中,我的设置如下:(多一点来指定密码设置)
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
Run Code Online (Sandbox Code Playgroud)
我很好奇可能是什么问题,因为它在 IIExpress 上正常工作,但在 Kestrel 上却没有。如果您需要我提供更多代码,请告诉我。对此事的任何帮助将不胜感激。谢谢。
编辑- Cookie 配置ConfigureServices:
/* 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;
});*/
Run Code Online (Sandbox Code Playgroud)
我基本上是按照本教程来创建这个的,我认为这不会有很多变化:Introduction to Identity on ASP.NET Core