Exemple #1
0
        public async Task <ActionResult <UserWithToken> > Login([FromBody] User user)
        {
            user = await _context.tblUser
                   .Where(u => u.EmailAddress == user.EmailAddress &&
                          u.Password == user.Password).FirstOrDefaultAsync();

            UserWithToken userWithToken = null;

            if (user != null)
            {
                RefreshToken refreshToken = GenerateRefreshToken();
                user.RefreshTokens.Add(refreshToken);
                await _context.SaveChangesAsync();

                userWithToken = new UserWithToken(user, null);
                userWithToken.RefreshToken = refreshToken.Token;
                //userWithToken.User_Info
                userWithToken.User_Info = (from itemUserInfo in _context.tblUserInfo

                                           join itemUser in _context.tblUser on itemUserInfo.UserId equals itemUser.UserId into ItemU
                                           from _User in ItemU.DefaultIfEmpty()

                                           join itemDept in _context.tblDepartment on itemUserInfo.DeptId.GetValueOrDefault() equals itemDept.DeptId into ItemD
                                           from _Dept in ItemD.DefaultIfEmpty()

                                           where itemUserInfo.UserId == user.UserId
                                           select new UserInfo
                {
                    CreatedDate = itemUserInfo.CreatedDate,
                    DeptId = itemUserInfo.DeptId.GetValueOrDefault(),
                    FirstName = itemUserInfo.FirstName,
                    LastName = itemUserInfo.LastName,
                    ModifiedDate = itemUserInfo.ModifiedDate,
                    UserDepartment = _Dept == null ? null : new Department()
                    {
                        DeptId = _Dept.DeptId, DeptName = _Dept.DeptName
                    },
                    UserId = itemUserInfo.UserId,
                    UserInfoId = itemUserInfo.UserInfoId,
                    EmailAddress = _User.EmailAddress,
                    IsActive = _User.IsActive
                }
                                           ).Cast <UserInfo>().FirstOrDefault();
            }

            if (userWithToken == null)
            {
                return(NotFound());
            }

            //sign your token here here..
            userWithToken.AccessToken = GenerateAccessToken(user.UserId);
            return(userWithToken);
        }
Exemple #2
0
        public async Task <ActionResult <IEnumerable <UserInfo> > > GettblUserInfo()
        {
            //return await _context.tblUserInfo.ToListAsync();

            var User_Info = await(from itemUserInfo in _context.tblUserInfo

                                  join itemUser in _context.tblUser on itemUserInfo.UserId equals itemUser.UserId into ItemU
                                  from _User in ItemU.DefaultIfEmpty()

                                  join itemDept in _context.tblDepartment on itemUserInfo.DeptId.GetValueOrDefault() equals itemDept.DeptId into ItemD
                                  from _Dept in ItemD.DefaultIfEmpty()


                                  select new UserInfo
            {
                CreatedDate    = itemUserInfo.CreatedDate,
                DeptId         = itemUserInfo.DeptId.GetValueOrDefault(),
                FirstName      = itemUserInfo.FirstName,
                LastName       = itemUserInfo.LastName,
                ModifiedDate   = itemUserInfo.ModifiedDate,
                UserDepartment = _Dept == null ? new Department()
                {
                    DeptId = 0, DeptName = ""
                } : new Department()
                {
                    DeptId = _Dept.DeptId, DeptName = _Dept.DeptName
                },
                UserId       = itemUserInfo.UserId,
                UserInfoId   = itemUserInfo.UserInfoId,
                EmailAddress = _User.EmailAddress,
                IsActive     = _User.IsActive
            }
                                  ).Cast <UserInfo>().ToListAsync();

            return(User_Info);
        }
Exemple #3
0
        private async Task <User> GetUserFromAccessToken(string accessToken)
        {
            try
            {
                var tokenHandler = new JwtSecurityTokenHandler();
                var key          = Encoding.ASCII.GetBytes(_jwtsettings.SecretKey);

                var tokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };

                SecurityToken securityToken;
                var           principle = tokenHandler.ValidateToken(accessToken, tokenValidationParameters, out securityToken);

                JwtSecurityToken jwtSecurityToken = securityToken as JwtSecurityToken;

                if (jwtSecurityToken != null && jwtSecurityToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256, StringComparison.InvariantCultureIgnoreCase))
                {
                    var userId = principle.FindFirst(ClaimTypes.Name)?.Value;


                    User user = await _context.tblUser
                                .Where(u => u.UserId == Convert.ToInt32(userId)
                                       ).FirstOrDefaultAsync();

                    user.User_Info = (from itemUserInfo in _context.tblUserInfo

                                      join itemUser in _context.tblUser on itemUserInfo.UserId equals itemUser.UserId into ItemU
                                      from _User in ItemU.DefaultIfEmpty()

                                      join itemDept in _context.tblDepartment on itemUserInfo.DeptId.GetValueOrDefault() equals itemDept.DeptId into ItemD
                                      from _Dept in ItemD.DefaultIfEmpty()

                                      where itemUserInfo.UserId == Convert.ToInt32(userId)
                                      select new UserInfo
                    {
                        CreatedDate = itemUserInfo.CreatedDate,
                        DeptId = itemUserInfo.DeptId.GetValueOrDefault(),
                        FirstName = itemUserInfo.FirstName,
                        LastName = itemUserInfo.LastName,
                        ModifiedDate = itemUserInfo.ModifiedDate,
                        UserDepartment = _Dept == null ? null : new Department()
                        {
                            DeptId = _Dept.DeptId, DeptName = _Dept.DeptName
                        },
                        UserId = itemUserInfo.UserId,
                        UserInfoId = itemUserInfo.UserInfoId,
                        EmailAddress = _User.EmailAddress,
                        IsActive = _User.IsActive
                    }
                                      ).Cast <UserInfo>().FirstOrDefault();

                    return(user);
                }
            }
            catch (Exception)
            {
                return(new User());
            }

            return(new User());
        }
Exemple #4
0
        public async Task <ActionResult <UserWithToken> > RegisterUser([FromBody] User user)
        {
            user.CreatedDate  = user.CreatedDate == null ? DateTime.Now : user.CreatedDate;
            user.ModifiedDate = user.ModifiedDate == null ? DateTime.Now : user.ModifiedDate;

            _context.tblUser.Add(user);
            await _context.SaveChangesAsync();



            if (user.User_Info != null)
            {
                _context.tblUserInfo.Add(new UserInfo()
                {
                    FirstName    = user.User_Info.FirstName,
                    LastName     = user.User_Info.LastName,
                    UserId       = user.UserId,
                    CreatedDate  = DateTime.Now,
                    ModifiedDate = DateTime.Now
                });

                await _context.SaveChangesAsync();
            }



            //load role for registered user
            //user = await _context.tblUser.Include(u => u.Role)
            //                            .Where(u => u.UserId == user.UserId).FirstOrDefaultAsync();

            UserInfo userInfo = (from itemUserInfo in _context.tblUserInfo

                                 join itemUser in _context.tblUser on itemUserInfo.UserId equals itemUser.UserId into ItemU
                                 from _User in ItemU.DefaultIfEmpty()

                                 join itemDept in _context.tblDepartment on itemUserInfo.DeptId equals itemDept.DeptId into ItemD
                                 from _Dept in ItemD.DefaultIfEmpty()

                                 where itemUserInfo.UserId == user.UserId
                                 select new UserInfo
            {
                CreatedDate = itemUserInfo.CreatedDate,
                DeptId = itemUserInfo.DeptId,
                FirstName = itemUserInfo.FirstName,
                LastName = itemUserInfo.LastName,
                ModifiedDate = itemUserInfo.ModifiedDate,
                UserDepartment = _Dept == null?null: new Department()
                {
                    DeptId = _Dept.DeptId, DeptName = _Dept.DeptName
                },
                UserId = itemUserInfo.UserId,
                UserInfoId = itemUserInfo.UserInfoId,
                EmailAddress = _User.EmailAddress,
                IsActive = _User.IsActive
            }
                                 ).Cast <UserInfo>().FirstOrDefault();

            UserWithToken userWithToken = null;

            if (user != null)
            {
                RefreshToken refreshToken = GenerateRefreshToken();
                user.RefreshTokens.Add(refreshToken);
                await _context.SaveChangesAsync();

                userWithToken = new UserWithToken(user, userInfo);
                userWithToken.RefreshToken = refreshToken.Token;
            }

            if (userWithToken == null)
            {
                return(NotFound());
            }

            //sign your token here here..
            userWithToken.AccessToken = GenerateAccessToken(user.UserId);
            return(userWithToken);
        }