Example #1
0
        public async Task <ActionResult> Login(Login login)
        {
            try
            {
                var Res = userService.Login(login.Username, login.Password);

                HttpContext.SetUserType(Res);

                var claims2 = new List <Claim>
                {
                    new Claim("user", Res.IsAdmin ? $"{Res.UserAdminRel?.Admin?.Firstname} {Res.UserAdminRel?.Admin?.Lastname}"
                    : Res.IsEmployee ? $"{Res.UserEmployeeRel?.Employee?.Firstname} {Res.UserEmployeeRel?.Employee?.Lastname}"
                    : $"{Res.GeneralUserRel?.GeneralUser?.Firstname} {Res.GeneralUserRel?.GeneralUser?.Lastname}"),
                    new Claim("role", "Member"),
                    new Claim("UserID", Res.ID.ToString())
                };
                var claimsIdentity = new ClaimsIdentity(claims2, CookieAuthenticationDefaults.AuthenticationScheme);

                var authProperties = new AuthenticationProperties
                {
                    IsPersistent = true,
                    RedirectUri  = this.Request.Host.Value
                };

                await HttpContext.SignInAsync(new ClaimsPrincipal(claimsIdentity));

                return(RedirectToAction("Index", "Books"));
            }
            catch (Exception Exc)
            {
                Exc.Log();
                ModelState.AddModelError(string.Empty, Exc.Message);
                return(View(login));
            }
        }