public async Task <IActionResult> changePassword(ChangePasswordReqDto changePassword)
        {
            ResponseDto res = new ResponseDto();

            try
            {
                var identity = User.Identity as ClaimsIdentity;
                if (identity != null)
                {
                    IEnumerable <Claim> claims = identity.Claims;
                    var  UserId    = claims.Where(p => p.Type == "userId").FirstOrDefault()?.Value;
                    var  CompanyId = claims.Where(p => p.Type == "companyId").FirstOrDefault()?.Value;
                    int  companyID = Convert.ToInt32(CompanyId);
                    int  userID    = Convert.ToInt32(UserId);
                    User userObj   = await uow.AccountRepository.FindUser(changePassword.userId);

                    if (userObj == null)
                    {
                        res.MessageType = "S";
                        res.Message     = MessageConstant.BadRequest;
                        return(Ok(new { ResponseDto = res }));
                    }
                    string newPwd = CommonFuntions.Encrypt(changePassword.newPassword.Trim());
                    userObj.password   = newPwd;
                    userObj.ModifiedBy = userID;
                    userObj.ModifiedOn = DateTime.Now;
                    userObj.userId     = Convert.ToInt32(changePassword.userId);
                    //  userObj.co = companyID;
                    userObj.ModifiedBy = userID;
                    userObj.ModifiedOn = DateTime.Now;
                    await uow.SaveAsync();

                    res.MessageType = "S";
                    res.Message     = MessageConstant.Password_changed_successfully;
                    return(Ok(new { ResponseDto = res }));
                }
                else
                {
                    res.MessageType = "E";
                    res.Message     = MessageConstant.BadRequest;
                    return(Ok(new { ResponseDto = res }));
                }
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, ex.Message);

                res.MessageType = "E";
                res.Message     = MessageConstant.Something_went_wrong;
                return(Ok(new { ResponseDto = res }));
            }
        }
        public async Task <IActionResult> IsValidPassword(string OldPassword)
        {
            ResponseResultDto res = new ResponseResultDto();

            try
            {
                var identity = User.Identity as ClaimsIdentity;
                if (identity != null)
                {
                    IEnumerable <Claim> claims = identity.Claims;
                    var  UserId    = claims.Where(p => p.Type == "userId").FirstOrDefault()?.Value;
                    var  CompanyId = claims.Where(p => p.Type == "companyId").FirstOrDefault()?.Value;
                    int  companyID = Convert.ToInt32(CompanyId);
                    int  userID    = Convert.ToInt32(UserId);
                    User userObj   = await uow.AccountRepository.FindUser(userID);

                    if ((0 != string.Compare(Convert.ToString(userObj.password).Trim().ToLower(), CommonFuntions.Encrypt(OldPassword.Trim()).ToLower(), false)))
                    {
                        res.MessageType = "oldpasswordNotMatch";
                        res.Message     = MessageConstant.InValidOldPwd;
                        return(Ok(new { ResponseResultDto = res }));
                    }
                    if (Convert.ToString(OldPassword) == Convert.ToString(CommonFuntions.Encrypt(OldPassword.Trim())))
                    {
                        res.MessageType = "oldNewSame";
                        res.Message     = MessageConstant.OldPwdNewPwdSame;
                        return(Ok(new { ResponseResultDto = res }));
                    }
                    res.MessageType = "S";
                    res.Message     = MessageConstant.Password_changed_successfully;
                    return(Ok(new { ResponseResultDto = res }));
                }
                else
                {
                    res.MessageType = "E";
                    res.Message     = MessageConstant.Something_went_wrong;
                    return(Ok(new { ResponseResultDto = res }));
                }
            }
            catch (Exception ex)
            {
                logger.Log(LogLevel.Error, ex.Message);

                res.MessageType = "E";
                res.Message     = MessageConstant.Something_went_wrong;
                return(Ok(new { ResponseResultDto = res }));
            }
        }
示例#3
0
        public async Task <IActionResult> Login([FromBody] TokenRequest model) // granttype = "refresh_token"
        {
            string ss = CommonFuntions.Encrypt("admin123");

            // We will return Generic 500 HTTP Server Status Error
            // If we receive an invalid payload
            if (model == null)
            {
                return(new StatusCodeResult(500));
            }

            switch (model.grantType)
            {
            case "password":
                return(await GenerateNewToken(model));

            case "refresh_token":
                return(await RefreshToken(model));

            default:
                // not supported - return a HTTP 401 (Unauthorized)
                return(new UnauthorizedResult());
            }
        }
        public async Task <IActionResult> AddEditUser([FromBody] UserReqDto userData)
        {
            ResponseResultDto res = new ResponseResultDto();

            UserResDto responseResult = new UserResDto();

            try
            {
                var identity = User.Identity as ClaimsIdentity;
                if (identity != null)
                {
                    IEnumerable <Claim> claims = identity.Claims;
                    var CompanyId = claims.Where(p => p.Type == "companyId").FirstOrDefault()?.Value;
                    int companyID = Convert.ToInt32(CompanyId);
                    var UserId    = claims.Where(p => p.Type == "userId").FirstOrDefault()?.Value;
                    int userID    = Convert.ToInt32(UserId);

                    if (uow.UserRepository.IsUserNameExists(userData.userName, userData.userId))
                    {
                        res.MessageType = "E";
                        res.Message     = String.Format(MessageConstant.RecordAlreadyExists, "username '" + userData.userName + "'");
                        return(Ok(new { ResponseResultDto = res }));
                    }

                    var user = mapper.Map <User>(userData);


                    user.ModifiedOn = DateTime.Now;
                    user.ModifiedBy = userID;

                    if (user.userId == 0)
                    {
                        user.CreatedOn = DateTime.Now;
                        user.CreatedBy = userID;
                        user.password  = CommonFuntions.Encrypt(user.password);
                        uow.UserRepository.AddUser(user);
                    }
                    else
                    {
                        User objUser = (await uow.UserRepository.FindUser(userData.userId)).FirstOrDefault();
                        user.password  = CommonFuntions.Encrypt(user.password);
                        user.CreatedOn = objUser.CreatedOn;
                        user.CreatedBy = objUser.CreatedBy;
                        uow.UserRepository.UpdateUser(user);
                        uow.UserCompanyRepository.DeleteUserCompanyByUserId(user.userId);
                    }
                    List <UserCompany> lstuserCompany = new List <UserCompany>();
                    foreach (var item in userData.userCompanyList)
                    {
                        UserCompany userCompany = new UserCompany();
                        userCompany.CompanyCode = item.companyCode;
                        userCompany.CompanyName = item.companyName;
                        userCompany.CreatedBy   = userID;
                        userCompany.CreatedOn   = DateTime.Now;
                        userCompany.userID      = user.userId;
                        lstuserCompany.Add(userCompany);
                    }
                    user.Usercompany = lstuserCompany;

                    uow.UserCompanyRepository.AddUserCompany(lstuserCompany);
                    await uow.SaveAsync();

                    List <User> lstUsers = new List <User>();
                    lstUsers = await uow.UserRepository.GetUsersAsync();

                    List <UserReqDto> lstreqDto = new List <UserReqDto>();
                    foreach (var item in lstUsers)
                    {
                        UserReqDto reqDto = new UserReqDto();
                        reqDto = mapper.Map <UserReqDto>(item);
                        List <UserCompanyList> lstUserCompany = new List <UserCompanyList>();
                        List <UserCompany>     lstcom         = await uow.UserCompanyRepository.GetUserCompanyByUserId(reqDto.userId);

                        foreach (var userCompany in lstcom)
                        {
                            UserCompanyList lst = new UserCompanyList();
                            lst.companyCode = userCompany.CompanyCode;
                            lst.companyName = userCompany.CompanyName;
                            lstUserCompany.Add(lst);
                        }
                        reqDto.userCompanyList = lstUserCompany;
                        lstreqDto.Add(reqDto);
                    }



                    responseResult.UserReqDto  = lstreqDto;
                    responseResult.messageType = "S";
                    if (userData.userId == 0)
                    {
                        responseResult.message = MessageConstant.RecordAdded;
                    }
                    else
                    {
                        responseResult.message = MessageConstant.RecordUpdated;
                    }
                }
                else
                {
                    responseResult.messageType = "E";
                    responseResult.message     = MessageConstant.Something_went_wrong;
                }
            }
            catch (Exception ex)
            {
                responseResult.messageType = "E";
                if (userData.userId == 0)
                {
                    responseResult.message = MessageConstant.FailedToAddRecord;
                }
                else
                {
                    responseResult.message = MessageConstant.FailedToUpdateRecord;
                }

                logger.Log(LogLevel.Error, ex.Message);
            }

            return(Ok(new { responseDto = responseResult }));
        }
 public async Task <User> Authenticate(string userName, string password)
 {
     return(await dc.Users.FirstOrDefaultAsync(x => x.userName == userName &&
                                               x.password == CommonFuntions.Encrypt(password)));
 }