예제 #1
0
        public async Task <LoginViewModel> Post(JObject json)
        {
            _invillaContext = new InvillaContext();

            try
            {
                var model = JsonConvert.DeserializeObject <LoginViewModel>(json.ToString());
                model.RegistrationDate = DateTime.Now;
                model.Password         = CryptoConfig.EncryptPassword(model.Password);

                var Users = new LoginsEntity
                {
                    FullName         = model.FullName,
                    RegistrationDate = model.RegistrationDate,
                    Password         = model.Password,
                    IdRole           = 1
                };
                _invillaContext.Add(Users);
                _invillaContext.SaveChanges();

                return(model);
            }
            catch (Exception ex)
            {
                return(new LoginViewModel
                {
                });
            }
        }
예제 #2
0
        public async Task <bool> GetLoginByName(JObject json)
        {
            try
            {
                _invillaContext = new InvillaContext();
                var model   = JsonConvert.DeserializeObject <LoginViewModel>(json.ToString());
                var loginDB = _invillaContext.Logins.Where(x => x.FullName == model.FullName).FirstOrDefault();
                var roleDB  = _invillaContext.Roles.Where(x => x.Id == model.IdRole).FirstOrDefault();

                var role = (string.IsNullOrEmpty(roleDB.Role)) ? "admin" : roleDB.Role;

                model.Password = CryptoConfig.EncryptPassword(model.Password);

                if (model.FullName == loginDB.FullName && model.Password == loginDB.Password)
                {
                    var claims = new List <Claim>()
                    {
                        new Claim("UserLoan", model.FullName),
                        new Claim("Password", model.Password),
                        new Claim("Role", role)
                    };

                    var token = JwtTokenUtils.GenerateInvillaUserToken(claims);
                    loginDB.Token = token;
                    _invillaContext.Update(loginDB);
                    _invillaContext.SaveChanges();

                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }