Exemple #1
0
        public async Task <IActionResult> Login(UserForLoginDto userForLoginDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userFromRepo = await _repo.Login(userForLoginDto.Username.ToLower(), userForLoginDto.Password, userForLoginDto.SchoolName1);

            if (userFromRepo == null)
            {
                _response.Success = false;
                _response.Message = CustomMessage.UserUnAuthorized;
                return(Ok(_response));
            }

            var     regNo = _config.GetSection("AppSettings:SchoolRegistrationNo").Value;
            dynamic schoolBranchDetails = await _repo.GetSchoolDetails(regNo, userFromRepo.SchoolBranchId);


            Claim[] claims = new[]
            {
                new Claim(Enumm.ClaimType.NameIdentifier.ToString(), userFromRepo.Id.ToString()),
                new Claim(Enumm.ClaimType.Name.ToString(), userFromRepo.Username),
                new Claim(Enumm.ClaimType.BranchIdentifier.ToString(), userForLoginDto.SchoolName1 > 0 ? userForLoginDto.SchoolName1.ToString() : schoolBranchDetails.branch.Id.ToString()),
                new Claim(ClaimTypes.Role, userFromRepo.Role),
                new Claim(Enumm.ClaimType.ExamType.ToString(), schoolBranchDetails.schoolExamType.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.UtcNow.AddDays(5),
                SigningCredentials = creds
            };
            var     tokenHandler   = new JwtSecurityTokenHandler();
            var     token          = tokenHandler.CreateToken(tokenDescriptor);
            var     NameIdentifier = Convert.ToInt32(claims.FirstOrDefault(x => x.Type.Equals(Enumm.ClaimType.NameIdentifier.ToString())).Value);
            var     Role           = claims.FirstOrDefault(x => x.Type.Equals(ClaimTypes.Role.ToString())).Value;
            dynamic CSName         = new ExpandoObject();

            if (Role == Enumm.UserType.Student.ToString() || Role == Enumm.UserType.Teacher.ToString())
            {
                CSName = (from u in _context.Users
                          join csUser in _context.ClassSectionUsers
                          on u.Id equals csUser.UserId
                          join cs in _context.ClassSections
                          on csUser.ClassSectionId equals cs.Id
                          where csUser.UserId == NameIdentifier &&
                          u.Role == Role
                          select new
                {
                    ClassSectionId = cs.Id,
                    ClassName = _context.Class.FirstOrDefault(m => m.Id == cs.ClassId && m.Active == true) != null ? _context.Class.FirstOrDefault(m => m.Id == cs.ClassId && m.Active == true).Name : "",
                    SectionName = _context.Sections.FirstOrDefault(m => m.Id == cs.SectionId && m.Active == true) != null ? _context.Sections.FirstOrDefault(m => m.Id == cs.SectionId && m.Active == true).SectionName : "",
                }).FirstOrDefault();
            }

            _response.Data = new
            {
                loggedInUserId   = claims.FirstOrDefault(x => x.Type.Equals(Enumm.ClaimType.NameIdentifier.ToString())).Value,
                loggedInUserName = claims.FirstOrDefault(x => x.Type.Equals(Enumm.ClaimType.Name.ToString())).Value,
                role             = claims.FirstOrDefault(x => x.Type.Equals(ClaimTypes.Role.ToString())).Value,
                schoolName       = schoolBranchDetails.school.Name,
                schoolLogo       = schoolBranchDetails.logo,
                token            = tokenHandler.WriteToken(token),
                classSectionName = GenericFunctions.IsPropertyExist(CSName, "ClassName") && GenericFunctions.IsPropertyExist(CSName, "SectionName") ? CSName.ClassName + " " + CSName.SectionName : "",
                classSectionId   = GenericFunctions.IsPropertyExist(CSName, "ClassSectionId") ? CSName.ClassSectionId : "",
                schoolExamType   = schoolBranchDetails.schoolExamType,
            };
            _response.Success = true;
            return(base.Ok(_response));
        }