Exemplo n.º 1
0
        private async Task <string> CreateToken(Engrisk.Models.Account accountFromDb)
        {
            var roles = await _userManager.GetRolesAsync(accountFromDb);

            var claims = new List <Claim> {
                new Claim(ClaimTypes.NameIdentifier, accountFromDb.Id.ToString()),
                new Claim(ClaimTypes.Name, accountFromDb.UserName)
            };

            foreach (var role in roles)
            {
                claims.Add(new Claim(ClaimTypes.Role, role));
            }
            var key             = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_config.GetSection("AppSettings:TokenSecret").Value));
            var creds           = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);
            var tokenDescriptor = new SecurityTokenDescriptor()
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddMinutes(60),
                SigningCredentials = creds
            };
            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(tokenDescriptor);

            return(tokenHandler.WriteToken(token));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register(AccountForRegisterDTO accountForRegister)
        {
            if (_repo.Exists(accountForRegister.Email) || _repo.Exists(accountForRegister.Username))
            {
                return(BadRequest("Username or email already registered"));
            }
            // byte[] passwordHashed, passwordSalt;
            // HashPassword(accountForRegister.Password, out passwordHashed, out passwordSalt);
            var account = new Engrisk.Models.Account()
            {
                UserName    = accountForRegister.Username,
                Fullname    = accountForRegister.Fullname,
                Address     = accountForRegister.Address,
                Email       = accountForRegister.Email,
                DateOfBirth = accountForRegister.DateOfBirth,
                PhoneNumber = accountForRegister.PhoneNumber
            };
            var accountCreated = await _userManager.CreateAsync(account, accountForRegister.Password);

            if (accountCreated.Succeeded)
            {
                foreach (var role in accountForRegister.Roles)
                {
                    await _userManager.AddToRoleAsync(account, role);
                }
                return(CreatedAtAction("GetAccountDetail", new { id = account.Id }, account));
            }
            return(BadRequest());
        }
Exemplo n.º 3
0
        public async Task <bool> DeletePhoto(Engrisk.Models.Account account)
        {
            var deleteParams = new DeletionParams(account.PublicId);

            if (_cloud.DeleteImage(account.PublicId))
            {
                account.PhotoUrl = null;
                account.PublicId = null;
                if (await(_repo.SaveAll()))
                {
                    return(true);
                }
                ;
                return(false);
            }
            return(false);
        }
Exemplo n.º 4
0
        public async Task <bool> DeletePhoto(Engrisk.Models.Account account)
        {
            var deleteParams = new DeletionParams(account.PublicId);
            var result       = _cloud.Destroy(deleteParams);

            if (result.Result != "OK")
            {
                return(false);
            }
            account.PhotoUrl = null;
            account.PublicId = null;
            if (await(_repo.SaveAll()))
            {
                return(true);
            }
            ;
            return(false);
        }