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.