コード例 #1
0
        public async override Task <CreateIdentityResult> Handle(CreateIdentityQuery request, CancellationToken cancellationToken)
        {
            var response = new CreateIdentityResult();

            try
            {
                var result = await _context.Identities.Where(e => e.Login == request.Login).FirstOrDefaultAsync();

                if (result == null)
                {
                    var hashResult      = PasswordEncrypter.Hash(request.Password);
                    var operationResult = await _context.AddAsync(new Domain.Identity.Identity
                    {
                        Login    = request.Login,
                        Password = hashResult.Hash,
                        Salt     = hashResult.Salt
                    });

                    if (operationResult.IsKeySet)
                    {
                        operationResult.Entity.Profile = new Domain.Identity.Profile();
                        response.Id = operationResult.Entity.Id;
                        await _context.SaveChangesAsync(cancellationToken);

                        response.Result  = true;
                        response.Message = $"Identity for \"{request.Login}\" created";
                    }
                }
                else
                {
                    response.Result  = false;
                    response.Message = $"\"{request.Login}\" already exists";
                }
            }
            catch (Exception e)
            {
                HandleException(response, e);
            }

            return(response);
        }
コード例 #2
0
        public async override Task <UpdateIdentityResult> Handle(UpdateIdentityQuery request, CancellationToken cancellationToken)
        {
            var response = new UpdateIdentityResult();

            try
            {
                var entity = await _context.Identities.FirstOrDefaultAsync(x => x.Id == request.Id);

                if (entity == null)
                {
                    response.Result  = false;
                    response.Message = $"Identity with id \"{request.Id}\" not found";
                }
                else
                {
                    if (!string.IsNullOrEmpty(request.Login))
                    {
                        entity.Login = request.Login;
                    }

                    if (!string.IsNullOrEmpty(request.Password))
                    {
                        entity.Password = PasswordEncrypter.Hash(request.Password, entity.Salt);
                    }

                    _context.Update(entity);
                    await _context.SaveChangesAsync();

                    response.Result  = true;
                    response.Message = $"Entity \"{entity.Login}\" updated";
                }
            }
            catch (Exception e)
            {
                HandleException(response, e);
            }

            return(response);
        }
コード例 #3
0
 public static bool SameAs(this string hashed, string raw, string salt, int hashLength, int iterations)
 {
     return(hashed.Equals(PasswordEncrypter.Hash(raw, salt, hashLength, iterations), StringComparison.InvariantCulture));
 }