Exemplo n.º 1
0
        public async Task <IActionResult> Login(StudentForLoginDto studentForLoginDto)
        {
            var studentFromRepo = await _repo.Login(studentForLoginDto.matNo.ToLower(), studentForLoginDto.password);

            if (studentFromRepo == null)
            {
                return(Unauthorized());
            }
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, studentFromRepo.Id.ToString()),
                new Claim(ClaimTypes.SerialNumber, studentFromRepo.matNo),
                new Claim(ClaimTypes.Name, studentFromRepo.firstname)
            };
            var key             = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));
            var creds           = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(3),
                SigningCredentials = creds
            };
            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(tokenDescriptor);

            return(Ok(new { token = tokenHandler.WriteToken(token) }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Login(StudentForLoginDto studentForLoginDto)
        {
            var userFromRepo = await _repo.Login(studentForLoginDto.MobileNo, studentForLoginDto.Password);

            if (userFromRepo == null)
            {
                return(Unauthorized());
            }


            var token = GenerateToken(userFromRepo.Name, userFromRepo.Role.RoleName);
            var user  = _mapper.Map <StudentForReturnDto>(userFromRepo);

            return(Ok(new {
                token,
                user
            }));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> StudentLogin(StudentForLoginDto userForLoginDto)
        {
            var userFromRepo = await _studentRepo.Login(userForLoginDto.Username, userForLoginDto.Password);

            if (userFromRepo == null)
            {
                return(Unauthorized());
            }

            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()),
                new Claim(ClaimTypes.Name, userFromRepo.Username)
            };

            var roles = await _context.Roles.Include(r => r.StudentRoles).Where(r => r.StudentRoles.Any(sr => sr.StudentId == userFromRepo.Id)).ToListAsync();

            foreach (var role in roles)
            {
                claims.Add(new Claim(ClaimTypes.Role, role.Name));
            }

            var key = new SymmetricSecurityKey(Encoding.UTF8
                                               .GetBytes(_config.GetSection("AppSettings:Token").Value));

            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = creds
            };

            var tokenHandler = new JwtSecurityTokenHandler();

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(Ok(new {
                token = tokenHandler.WriteToken(token)
            }));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> LoginStudent([FromBody] StudentForLoginDto loginUser)
        {
            var users = await _context.Student
                        .Where(s => s.Number == loginUser.number && s.Password == loginUser.password).ToListAsync();

            if (users.Count == 0)
            {
                return(Unauthorized());
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:Token").Value);
            var user            = users.First();
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                    new Claim(ClaimTypes.Email, user.Mail),
                    new Claim(ClaimTypes.Name, user.Name + " " + user.Surname)
                }),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key),
                                                            SecurityAlgorithms.HmacSha512Signature)
            };

            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            var response = new LoginStudentResponse
            {
                Id    = user.Id,
                Name  = user.Name + " " + user.Surname,
                Token = tokenString
            };

            return(Ok(response));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Login(StudentForLoginDto studentForLoginDto)
        {
            var studentFromRepository = await _repository.Login(studentForLoginDto.Username, studentForLoginDto.Password);


            if (studentFromRepository == null)
            {
                return(Unauthorized("Niepoprawna nazwa użytkownika, lub hasło"));
            }


            //create Token
            var claims = new[]
            {
                new Claim("LastName", studentFromRepository.LastName),
                new Claim("FirstName", studentFromRepository.FirstName),
                new Claim("ClassName", studentFromRepository.SchoolClass.Name),
                new Claim("UserRole", studentFromRepository.UserRole),
                new Claim("StudentID", studentFromRepository.StudentID.ToString()),
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));

            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddHours(10),
                SigningCredentials = creds
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(tokenDescriptor);

            return(Ok(new { token = tokenHandler.WriteToken(token) }));
        }