コード例 #1
0
        public async Task <IActionResult> Login(UserLoginModel model)
        {
            if (ModelState.IsValid)
            {
                var user = _userService.CheckUser(model.Email, model.Password);
                if (user != null)
                {
                    var claims = new List <Claim>();

                    var role = _genericRoleService.GetById(user.RoleId);

                    claims.Add(new Claim(ClaimTypes.Role, role.Name));

                    claims.Add(new Claim(ClaimTypes.Name, user.Name + " " + user.Surname));

                    var claimsIdentity = new ClaimsIdentity(
                        claims, CookieAuthenticationDefaults.AuthenticationScheme);

                    await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal(claimsIdentity)
                        );

                    if (role.Id == 2)
                    {
                        return(RedirectToAction("Index", "Voyage"));
                    }
                    if (role.Id == 1)
                    {
                        return(RedirectToAction("Index", "Admin"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Kullanıcı adı veya şifre hatalı");
                }
            }



            return(View(model));
        }