예제 #1
0
        public async Task <IActionResult> Login(GirisCO request)
        {
            KullaniciDTO user = _kullaniciService.GetByKullanici(request.Email, request.Sifre);

            if (user == null)
            {
                return(View("Error"));
            }

            var giris = new GirisDTO()
            {
                KullaniciId = user.Id,
                Durum       = true,
                Aktif       = true,
                Silindi     = false
            };
            var girisId = _girisService.Create(giris);

            if (user != null)
            {
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, user.Id.ToString()),
                    new Claim(ClaimTypes.NameIdentifier, user.Ad + " " + user.Soyad),
                    new Claim(ClaimTypes.Role, user.YetkiId.ToString())
                };

                var userIdentity = new ClaimsIdentity(claims, "login");

                ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
                await HttpContext.SignInAsync(principal);

                if (user.YetkiId == (int)Yetkiler.ADMIN)
                {
                    ViewBag.User = user;
                    return(RedirectToAction("Index", "Admin"));
                }

                if (user.YetkiId == (int)Yetkiler.TEACHER)
                {
                    ViewBag.User = user;
                    return(RedirectToAction("Index", "Home"));
                }

                ViewBag.User = user;
                return(RedirectToAction("Index", "Home"));
            }
            return(View());
        }
예제 #2
0
        public ActionResult Giris([FromForm] LoginCO request)
        {
            if (request == null)
            {
                throw new PetClinicAppointmentBadRequestException("Oturum açmak için kullanıcı adı ve şifresini giriniz!");
            }

            if (string.IsNullOrEmpty(request.Email) || string.IsNullOrEmpty(request.Password))
            {
                throw new PetClinicAppointmentBadRequestException("Eksik parametre girdiniz!");
            }

            UserDTO user = null;

            user = _userService.GetByKullaniciAdiAndSifre(request.Email, request.Password);

            if (user == null)
            {
                throw new PetClinicAppointmentUnauthorizedException("Email or password incorrect!");
            }

            user.TuzlamaDegeri = "";
            user.Password      = "";

            var claims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
                new Claim(JwtRegisteredClaimNames.UniqueName, user.Id.ToString()),
                new Claim(JwtRegisteredClaimNames.Jti, user.Id.ToString()),
                new Claim("KullaniciGuid", user.Guid.ToString())
            };
            // Giriş yapan kullanıcının token kaydı veritabanına ekleyelim
            var token = new JwtSecurityToken
                        (
                issuer: Configuration["Jwt:Issuer"],
                audience: Configuration["Jwt:Audience"],
                claims: claims,
                expires: DateTime.UtcNow.AddMonths(1),
                notBefore: DateTime.UtcNow,
                signingCredentials: new SigningCredentials(
                    new SymmetricSecurityKey(
                        Encoding.UTF8.GetBytes(
                            Configuration[
                                "Jwt:SigningKey"])),
                    SecurityAlgorithms.HmacSha256)
                        );
            var tokenn = new JwtSecurityTokenHandler().WriteToken(token);

            var giris = new GirisDTO()
            {
                Guid        = Guid.NewGuid(),
                UserId      = user.Id,
                Token       = tokenn,
                Durum       = true,
                Actived     = true,
                Deleted     = false,
                CreatedDate = DateTime.Now
            };
            var girisId = _girisService.Create(giris);

            if (girisId <= 0)
            {
                throw new PetClinicAppointmentBadRequestException("Login Failed!");
            }
            var sonuc = new ResultDTO();

            if (girisId > 0)
            {
                sonuc.Message.Add(new MessageDTO()
                {
                    Code        = HttpStatusCode.OK,
                    Status      = EDurum.SUCCESS,
                    Description = "Login Success"
                });
                sonuc.Status = sonuc.Message.OrderBy(x => x.Code).FirstOrDefault().Status;
                sonuc.Data   = new
                {
                    token   = tokenn,
                    account = new
                    {
                        user.Guid,
                        user.Email,
                        user.Name,
                        user.Surname,
                        role = user.Yetki != null ? new { user.Yetki.Guid, user.Yetki.Name, user.Yetki.Description } : null,
                    }
                };
            }
            return(Ok(sonuc));
        }