Пример #1
0
        public async Task <ActionResult <string> > Update(UserDataForUpdate userDataForUpdate, CancellationToken cancellationToken = default)
        {
            return(await _exceptionHandler.HandleExceptionAsync <ActionResult <string> >(async c =>
            {
                var nameIdentifierClaim = _user.Claims.SingleOrDefault(x => x.Type == ClaimTypes.NameIdentifier);
                var userId = Convert.ToInt32(nameIdentifierClaim.Value);
                if (userDataForUpdate.Id != userId)
                {
                    return BadRequest("User can update only his or her data and not anyone else's data");
                }
                var user = await _userQueryableDomainService.Get().FirstOrDefaultAsync(x => x.Id == userId);

                if (userDataForUpdate.Password.IsNotNullOrEmpty())
                {
                    byte[] passwordHash = null;
                    byte[] passwordSalt = null;
                    PasswordUtility.CreatePasswordHash(userDataForUpdate.Password, out passwordHash, out passwordSalt);
                    user.PasswordHash = passwordHash;
                    user.PasswordSalt = passwordSalt;
                }
                user.Name = userDataForUpdate.Name;
                user.Address = userDataForUpdate.Address;
                user.State = userDataForUpdate.State;
                user.Country = userDataForUpdate.Country;
                user.Email = userDataForUpdate.Email;
                user.PAN = userDataForUpdate.PAN;
                user.ContactNumber = userDataForUpdate.ContactNumber;

                await _userCommandDomainServiceAsync.UpdateAsync(user, c).ConfigureAwait(false);
                return Ok("User data updated successfully");
            }, cancellationToken));
        }
Пример #2
0
 public async Task <ActionResult <string> > Register(User user, CancellationToken cancellationToken = default)
 {
     return(await _exceptionHandler.HandleExceptionAsync <ActionResult <string> >(async c =>
     {
         User userInDB = await _userQueryableDomainService.Get().SingleOrDefaultAsync(x => x.LoginData.UserName.ToLower().Trim() == user.LoginData.UserName.ToLower().Trim(), c).ConfigureAwait(false);
         if (userInDB.IsNotNull())
         {
             return BadRequest("A user with userName : "******", already exists.Please use a different user name.");
         }
         byte[] passwordHash = null;
         byte[] passwordSalt = null;
         PasswordUtility.CreatePasswordHash(user.LoginData.Password, out passwordHash, out passwordSalt);
         user.PasswordHash = passwordHash;
         user.PasswordSalt = passwordSalt;
         await _userCommandDomainServiceAsync.InsertAsync(user, c).ConfigureAwait(false);
         return Ok("User created successfully");
     }, cancellationToken));
 }