Пример #1
0
        public async Task <ServiceResponse> ActivateAccountAsync(string email, string token)
        {
            var user = await _userManager.FindByEmailAsync(_cryptoProvider.EncryptPrivate(email)).ConfigureAwait(false);

            if (user == null)
            {
                return(new ServiceResponse("User not found."));
            }

            var result = await _userManager.ConfirmEmailAsync(user, WebUtility.UrlDecode(token)).ConfigureAwait(false);

            if (!result.Succeeded)
            {
                return(new ServiceResponse($"Account activation failed: {string.Join(", ", result.Errors.Select(e => e.Description))}"));
            }

            if (!await _contextProvider.Context.Tenants.AnyAsync(t => t.UserId == user.Id).ConfigureAwait(false))
            {
                await _contextProvider.ExecuteTransactionAsync(async context =>
                {
                    var tenant = new Tenant
                    {
                        ExpirationDateUtc = DateTime.UtcNow.AddYears(100),
                        IsBlocked         = false,
                        UserId            = user.Id,
                        TenantKey         = WebUtility.UrlDecode(user.Id.Hash())
                    };
                    await context.Tenants.AddAsync(tenant);
                    await context.SaveChangesAsync();
                    return(new ServiceResponse());
                }).ConfigureAwait(false);
            }
            return(new ServiceResponse());
        }
Пример #2
0
        public virtual async Task <EntityResponse <TDto> > CreateAsync(TDto dto)
        {
            if (await _contextProvider.Context.Set <TEntity>().AnyAsync(e => e.Id == dto.Id).ConfigureAwait(false))
            {
                return(new EntityResponse <TDto>($"The entity {nameof(TEntity).ToLower()} already exists with id: {dto.Id}"));
            }

            var entity = Mapper.Map <TEntity>(dto);

            return((EntityResponse <TDto>) await _contextProvider.ExecuteTransactionAsync(async context =>
            {
                await context.Set <TEntity>().AddAsync(entity).ConfigureAwait(false);
                await context.SaveChangesAsync().ConfigureAwait(false);

                return new EntityResponse <TDto>(Mapper.Map <TDto>(entity));
            }).ConfigureAwait(false));
        }