Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Cookie身份验证在ASP.NET核心应用程序中不起作用_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# Cookie身份验证在ASP.NET核心应用程序中不起作用

C# Cookie身份验证在ASP.NET核心应用程序中不起作用,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我正在尝试在.NETCore3.1中开发一个项目。我正在尝试在我的项目中实现基于cookie的身份验证。我的登录功能是: [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Login(UserLoginModel userModel) { if (!ModelState.IsValid) { return View(userModel); }

我正在尝试在.NETCore3.1中开发一个项目。我正在尝试在我的项目中实现基于cookie的身份验证。我的登录功能是:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(UserLoginModel userModel)
{
    if (!ModelState.IsValid)
    {
        return View(userModel);
    }

    if (userModel.Email == "admin@test.com" && userModel.Password == "123")
    {
        var identity = new ClaimsIdentity(IdentityConstants.ApplicationScheme);
        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "User Id"));
        identity.AddClaim(new Claim(ClaimTypes.Name, "User Name"));

        var principal = new ClaimsPrincipal(identity);

        await HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, principal);

        return RedirectToAction(nameof(HomeController.Index), "Home");
    }
    else
    {
        ModelState.AddModelError("", "Invalid UserName or Password");
        return View();
    }
}
但问题是每次我尝试登录时,下面的登录操作方法代码中会出现以下异常

等待HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, (校长)

发生的异常如下所示:

InvalidOperationException:未找到登录身份验证处理程序 已为“Identity.Application”计划注册。登记的 登录方案包括:Cookies。你忘了打电话了吗 AddAuthentication().AddCookies(“Identity.Application”,…)? Microsoft.AspNetCore.Authentication.AuthenticationService.SignInAsync(HttpContext 上下文、字符串方案、ClaimsPrincipal主体、, AuthenticationProperties(属性) _01\u AccountController.cs中的AuthenticationDemo.Controller.AccountController.Login(UserLoginModel userModel) + 等待HttpContext.SignInAsync(IdentityConstants.ApplicationScheme, 校长)

谁能给我一些解决这个问题的建议吗

没有为方案“Identity.Application”注册登录身份验证处理程序。注册的登录方案为:Cookies

请使用
CookieAuthenticationDefaults.AuthenticationScheme
将其指定,如下所示

if (userModel.Email == "admin@test.com" && userModel.Password == "123")
{
    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "User Id"));
    identity.AddClaim(new Claim(ClaimTypes.Name, "User Name"));

    var principal = new ClaimsPrincipal(identity);

    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

    return RedirectToAction(nameof(HomeController.Index), "Home");
}
有关更多信息,请检查:

测试结果


我将在这里留下这条评论,以防对任何人有所帮助

我花了两天的时间尝试获取cookies身份验证,然后在我的.NETCore3.1站点上只使用了一个基本会话身份验证登录页面

原来问题是我的广告平台(Ezoic)导致了这个问题。他们基本上使用某种代理系统,请求进入他们的服务器,然后他们的服务器向我的服务器发出请求(反向代理?)。无论如何,如果您使用的是CDN或ad服务器或类似的东西,那么这可能是一个问题

我的另一个线索是,我的站点在本地主机上运行良好,但在生产服务器上运行不好


为了解决这个问题,广告公司说我可以在我的网站上添加一个X-Forwarded-For标题,但我一直无法实现这一点,尽管我遵循了在Startup.cs中添加适当行的说明。

这个问题是第一个有可行解决方案的问题
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseStaticFiles();
    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}
if (userModel.Email == "admin@test.com" && userModel.Password == "123")
{
    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "User Id"));
    identity.AddClaim(new Claim(ClaimTypes.Name, "User Name"));

    var principal = new ClaimsPrincipal(identity);

    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

    return RedirectToAction(nameof(HomeController.Index), "Home");
}