0

In The Startup.cs at the 'ConfigureServices' I am Using

 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").

This is my /Employees/Details/ action ->

    [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.

2 Answers2

1

I have solved this problem, by watching the related question provided by stack-overflow. Visit How do I setup multiple auth schemes in ASP.NET Core 2.0?. Just need to Define the AuthenticationSchemes while using SignInAsync function.

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

Try debugging the methods.

It seems like it doesnt hit the Details method.

Try also replacing

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

with

return RedirectToAction("Details", "Employees", new {id = employee.Email});
Flori Bruci
  • 436
  • 4
  • 11
  • It hits the Details method but it redirects the login page autometically. Basically I have used two AuthenticationScheme. Because of the fact I have not defined which Scheme I will use before login thats the reason why it autometically redirecting me to login page for security purpose. Thanks for your contribution. – Shamsi Shakeeb May 21 '20 at 07:29