I have a controller that needs to redirect views based on user roles:
[Authorize(Roles = "Projects,Projects-Manager")]
public IActionResult Projects()
{
return View();
}
[Authorize(Roles = "Procurment,Procurment-Manager")]
public IActionResult Procurment()
{
return View();
}
[Authorize(Roles = "HR,HR-Manager")]
public IActionResult HR()
{
return View();
}
public IActionResult Index()
{
return View();
}
//GET: Login
[HttpGet]
public IActionResult Login(string returnUrl)
{
ViewData["ReturnUrl"] = returnUrl;
return View();
}
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(UserLoginModel userModel)
{
if (!ModelState.IsValid)
{
return View(userModel);
}
var result = await _signInManager.PasswordSignInAsync(userModel.UserName, userModel.Password, userModel.RememberMe, false);
if (result.Succeeded && HttpContext.User.IsInRole("HR,HR-Manager"))
{
return Redirect(nameof(HR));
}
else if (result.Succeeded && HttpContext.User.IsInRole("Projects,Projects-Manager"))
{
return Redirect(nameof(Projects));
}
else if (result.Succeeded && HttpContext.User.IsInRole("Procurmant,Procurmant-Manager"))
{
return RedirectToAction(nameof(Procurment));
}
else
{
ModelState.AddModelError("", "Invalid UserName or Password");
return View();
}
}
and here is the startup.cs file.
services.AddIdentity<User, IdentityRole>(opt =>
{
opt.User.RequireUniqueEmail = true;
opt.User.AllowedUserNameCharacters = "";
opt.Password.RequiredLength = 7;
opt.Password.RequireDigit = false;
opt.Password.RequireUppercase = false;
})
.AddEntityFrameworkStores<ApplicationContext>();
services.AddScoped<IUserClaimsPrincipalFactory<User>, CustomClaimsFactory>();
services.AddAutoMapper(typeof(Startup));
services.AddControllersWithViews();
}
I encountered an error when trying to access my localhost page. The error message reads:
"This localhost page can’t be foundNo webpage was found for the web address: https://localhost:7044/Account/Login?ReturnUrl=%2FHome%2FLogin HTTP ERROR 404"
I've made adjustments to the code as suggested by solutions on StackOverflow, but I continue to face the same issue. Is there any additional configuration that needs to be added in the startup file?
UPDATE: After correcting a mistake in the name of the Login view, I'm still facing issues with redirection and now encountering HTTP ERROR 405.