Код: Выделить всё
[Area("User Panel")]
[Authorize]
public class UserPanelController : Controller
{
private iUserServices _iUserService;
public UserPanelController(iUserServices iUserService)
{
_iUserService = iUserService;
}
public IActionResult Profile()
{
var userPanel = _iUserService.GetInformations(User.Identity.Name);
var resetPassword = new ResetPasswordViewModel();
var userpanel = new UserPanelVM
{
UserPanelViewModel = userPanel,
ResetPasswordViewModel = resetPassword
};
return View(userpanel);
}
}
// Login
[HttpPost]
public IActionResult Login(LoginViewModel login)
{
if (!ModelState.IsValid)
{
return View(login);
}
var user = _iUserServices.LoginUser(login);
//Authentication check
if(user != null)
{
var claim = new List()
{
new Claim(ClaimTypes.NameIdentifier, user.userId.ToString()),
new Claim(ClaimTypes.Name, user.userName)
};
var identity = new ClaimsIdentity(claim, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
var properties = new AuthenticationProperties()
{
IsPersistent = login.rememberMe
};
HttpContext.SignInAsync(principal, properties);
ViewBag.IsSuccess = true;
return RedirectToAction("Profile", "Home", new { area = "User Panel" });
}
else
{
ModelState.AddModelError("userEmail", "کاربر با مشخصات وارد شده یافت نشد!");
}
return View(login);
}
// Program.cs
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name:"default",
pattern:"{controller=Home}/{action=HomePage}/{id?}");
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Profile}/{id?}");
endpoints.MapRazorPages();
endpoints.MapControllers();
});
Подробнее здесь: https://stackoverflow.com/questions/791 ... ddress-htt
Мобильная версия