简体   繁体   English

成功登录尝试后,系统再次重定向到 ASP.NET CORE 中的登录页面

[英]After Successfull Login Attempt the System is redirecting to Login Page Again in ASP.NET CORE

In The Startup.cs at the 'ConfigureServices' I am Using在我正在使用的“ConfigureServices”的 Startup.cs

 services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Boss";

        })
        .AddCookie("Employee", options =>
        {

            options.Cookie.Name = "Employee.Says";
            options.LoginPath = "/Employees/Login";

        })
        .AddCookie("Boss", options =>
         {
             options.Cookie.Name = "Boss.Says";
             options.LoginPath = "/Boss/Login";

         });

Then in the Login Action I have Written this Code然后在登录操作中我写了这段代码

    [HttpPost]
    [ValidateAntiForgeryToken]
    [AllowAnonymous]
    public async Task<IActionResult> Login([Bind("Email", "Password")]Employee employee)
    {
        var data = await _context.employee.Where((x => x.Email == employee.Email && x.Password == employee.Password)).FirstOrDefaultAsync<Employee>();
        ClaimsIdentity identity = null;

        if (data != null)
        {

            identity = new ClaimsIdentity(new[] {
            new Claim(ClaimTypes.Email,employee.Email),
            new Claim(ClaimTypes.Role,"Employee")


        }, CookieAuthenticationDefaults.AuthenticationScheme);



            var principal = new ClaimsPrincipal(identity);
            await HttpContext.SignInAsync(principal);

            HttpContext.Session.SetString(SessionKey, employee.Email);

            return Redirect("~/Employees/Details/" + employee.Email);
        }

        else
        {
            ModelState.AddModelError(string.Empty, "Invalid Login");
        }


        return View(employee);
    }

But After Successfull Login still the System Redirecting me to ("/Employees/Login").但是在成功登录后系统仍然将我重定向到(“/Employees/Login”)。

This is my /Employees/Details/ action ->这是我的 /Employees/Details/ 操作 ->

    [Authorize(Roles = "Employee", AuthenticationSchemes = "Employee")]
    public async Task<IActionResult> Details(string id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var employee = await _context.employee
            .FirstOrDefaultAsync(m => m.Email == id);
        if (employee == null)
        {
            return NotFound();
        }

        return View();
    }

I am not understanding how to fix this issue and whats really going on.我不明白如何解决这个问题以及到底发生了什么。

I have solved this problem, by watching the related question provided by stack-overflow.通过查看 stack-overflow 提供的相关问题,我已经解决了这个问题。 Visit How do I setup multiple auth schemes in ASP.NET Core 2.0?访问如何在 ASP.NET Core 2.0 中设置多个身份验证方案? . . Just need to Define the AuthenticationSchemes while using SignInAsync function.只需在使用 SignInAsync function 时定义 AuthenticationSchemes。

await HttpContext.SignInAsync("Employee",principal);

Try debugging the methods.尝试调试方法。

It seems like it doesnt hit the Details method.似乎它没有击中 Details 方法。

Try also replacing也尝试更换

return Redirect("~/Employees/Details/" + employee.Email);

with

return RedirectToAction("Details", "Employees", new {id = employee.Email});

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

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