Example #1
0
        public AuthenticateResponse Authenticate(AuthenticateRequest model, string ipAddress)
        {
            var Users = _userBs.GetAll();
            var user  = Users.SingleOrDefault(x => x.Username == model.Username && x.Password == model.Password);

            // return null if user not found
            if (user == null)
            {
                return(null);
            }
            var Usermodal = new WebApi.Models.User()
            {
                Username  = user.Username,
                Id        = user.Id,
                Password  = user.Password,
                FirstName = user.FirstName,
                LastName  = user.LastName
            };
            // authentication successful so generate jwt token
            var token        = GenerateJwtToken(Usermodal);
            var refreshToken = refreshTokenGenerator.generateRefreshToken(ipAddress);

            user.RefreshTokens.Add(new DAL.Models.RefreshToken()
            {
                Token           = refreshToken.Token,
                Expires         = refreshToken.Expires,
                IsExpired       = refreshToken.IsExpired,
                Created         = refreshToken.Created,
                CreatedByIp     = refreshToken.CreatedByIp,
                Revoked         = refreshToken.Revoked,
                RevokedByIp     = refreshToken.RevokedByIp,
                ReplacedByToken = refreshToken.ReplacedByToken,
                IsActive        = refreshToken.IsActive,
                UserId          = user.Id
            });

            var success = _userBs.Update(user);

            //System.Threading.Thread.Sleep(2000);
            var xxx = _session?.GetString(user?.Username);

            // first tokens
            //_session.SetString(user.Username, refreshToken.Token);



            //if (UsersRefreshTokens.ContainsKey(user.Username))
            //{
            //    UsersRefreshTokens[user.Username] = refreshToken.Token;
            //}
            //else
            //{
            //    UsersRefreshTokens.Add(user.Username, refreshToken.Token);
            //}
            return(new AuthenticateResponse(Usermodal, token, refreshToken.Token));
        }
Example #2
0
        public AuthenticateResponse Refresh(RefreshCred refreshCred, string ipAddress)
        {
            var tokenHandler = new JwtSecurityTokenHandler();
            var key          = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var pricipal     = tokenHandler.ValidateToken(refreshCred.JwtToken, new Microsoft.IdentityModel.Tokens.TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = new SymmetricSecurityKey(key),
                ValidateIssuer           = false,
                ValidateAudience         = false,
                ValidateLifetime         = false //here we are saying that we don't care about the token's expiration date

                                                 // set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later)
                                                 //  ClockSkew = TimeSpan.Zero
            }, out SecurityToken validatedToken);

            var jwtToken = validatedToken as JwtSecurityToken;
            var lifeTime = new JwtSecurityTokenHandler().ReadToken(refreshCred.JwtToken).ValidTo;

            if (jwtToken == null || !jwtToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new SecurityTokenException("Invalid token passed");
            }
            var userName = pricipal.Identity.Name;
            var userId   = Convert.ToInt32(pricipal.FindFirst("id").Value);
            var Users    = userBs.GetAll();
            var user     = Users.SingleOrDefault(x => x.Id == userId);

            var sessionrefreshtoken = user?.RefreshTokens?.Where(x => x.Token.Trim().Equals(refreshCred?.RefreshToken?.Trim()))?.FirstOrDefault()?.Token;

            //_session?.GetString(userName)?.ToString();
            if (sessionrefreshtoken == null)
            {
                throw new SecurityTokenException("Invalid token passed!");
            }

            ///jWTAuthenticationManager.UsersRefreshTokens[userName] need to fix why not working
            //if (  user.RefreshTokens.Single(x => x.Token.Trim().Equals(refreshCred.RefreshToken.Trim())
            //{
            // //   throw new SecurityTokenException("Invalid token passed!")
            //}

            return(jWTAuthenticationManager.Authenticate(userName, pricipal.Claims.ToArray(), ipAddress, refreshCred));
        }
Example #3
0
        public ActionResult HR(UserListVM vm)
        {
            List <UserListVM> list = _us.GetAll()
                                     .Select(x => new UserListVM()
            {
                Photo    = x.Photo,
                FName    = x.FName,
                LName    = x.LName,
                Email    = x.Email,
                Phone    = x.Phone,
                Created  = x.Created,
                IsActive = x.IsActive,
                UserName = x.UserName,
                RoleName = _rs.GetAll()
                           .Where(y => y.Id == x.RoleId)
                           .Select(y => y.Name)
                           .FirstOrDefault(),
                PersonnelCode = x.Id
            }).ToList();

            return(View(list));
        }
Example #4
0
        public ActionResult Task()
        {
            if (SessionManager.ActiveUser.Id == 1 || SessionManager.ActiveUser.Id == 5)
            {
                List <TaskListVM> list = _ts.GetAll()
                                         .Where(x => x.IsActive == true)
                                         .OrderByDescending(x => x.Modified)
                                         .Select(x => new TaskListVM()
                {
                    Id      = x.Id,
                    UserId  = x.UserId,
                    FromAss = _us.GetAll()
                              .Where(y => y.Id == x.CreatedBy)
                              .Select(y => y.Photo)
                              .FirstOrDefault(),
                    ToAss = _us.GetAll()
                            .Where(y => y.Id == x.UserId)
                            .Select(y => y.Photo)
                            .FirstOrDefault(),
                    Created     = x.Created,
                    Name        = x.Name,
                    State       = x.State,
                    Modified    = x.Modified,
                    Description = x.Description,
                    ShowToAss   = true,
                    ShowFromAss = true
                }).ToList();
                return(View(list));
            }

            else if (SessionManager.ActiveUser.Id == 6 || SessionManager.ActiveUser.Id == 7 || SessionManager.ActiveUser.Id == 1002 || SessionManager.ActiveUser.Id == 1003)
            {
                List <TaskListVM> list = _ts.GetAll()
                                         .Where(x => x.IsActive == true && x.CreatedBy == SessionManager.ActiveUser.Id || x.ToAss == SessionManager.ActiveUser.Photo)
                                         .OrderByDescending(x => x.Created)
                                         .Select(x => new TaskListVM()
                {
                    Id      = x.Id,
                    UserId  = x.UserId,
                    FromAss = _us.GetAll()
                              .Where(y => y.Id == x.CreatedBy)
                              .Select(y => y.Photo)
                              .FirstOrDefault(),
                    ToAss = _us.GetAll()
                            .Where(y => y.Id == x.UserId)
                            .Select(y => y.Photo)
                            .FirstOrDefault(),
                    Created     = x.Created,
                    Name        = x.Name,
                    State       = x.State,
                    Modified    = x.Modified,
                    Description = x.Description,
                    ShowToAss   = true,
                    ShowFromAss = false
                }).ToList();
                return(View(list));
            }

            else
            {
                List <TaskListVM> list = _ts.GetAll()
                                         .Where(x => x.IsActive == true && x.CreatedBy != SessionManager.ActiveUser.Id)
                                         .OrderByDescending(x => x.Created)
                                         .Select(x => new TaskListVM()
                {
                    Id      = x.Id,
                    UserId  = x.UserId,
                    FromAss = _us.GetAll()
                              .Where(y => y.Id == x.CreatedBy)
                              .Select(y => y.Photo)
                              .FirstOrDefault(),
                    ToAss = _us.GetAll()
                            .Where(y => y.Id == x.UserId)
                            .Select(y => y.Photo)
                            .FirstOrDefault(),
                    Created     = x.Created,
                    Name        = x.Name,
                    State       = x.State,
                    Modified    = x.Modified,
                    Description = x.Description,
                    ShowToAss   = false,
                    ShowFromAss = true
                }).ToList();
                return(View(list));
            }
        }
Example #5
0
        public IActionResult GetAll()
        {
            var users = _userBs.GetAll();

            return(Ok(users));
        }