Exemplo n.º 1
0
        public async Task <IDataResponse <SelfViewModel> > LoginAsync(
            LoginRequest model, CancellationToken token = default)
        {
            model.ArgumentNullCheck(nameof(model));

            using (var scope = TransactionFactory.CreateTransaction())
            {
                var result = await service.LoginAsync(model, token)
                             .ConfigureAwaitFalse();

                scope.Complete();

                return(result);
            }
        }
Exemplo n.º 2
0
        public async Task <IDataResponse <SelfViewModel> > LoginAsync(
            LoginRequest model, CancellationToken token = default)
        {
            model.ArgumentNullCheck(nameof(model));

            var signInResult = await signInManager.PasswordSignInAsync(
                model.Email, model.Password, model.IsPersistent, false)
                               .ConfigureAwaitFalse();

            if (signInResult.Succeeded)
            {
                var identityUser = await signInManager.UserManager.FindByEmailAsync(model.Email)
                                   .ConfigureAwaitFalse();

                if (!await signInManager.UserManager.IsInRoleAsync(identityUser, Roles.Administrators))
                {
                    if (!await roleManager.RoleExistsAsync(Roles.Administrators).ConfigureAwaitFalse())
                    {
                        await roleManager.CreateAsync(new IdentityRole <int>(Roles.Administrators))
                        .ConfigureAwaitFalse();
                    }

                    await signInManager.UserManager.AddToRoleAsync(identityUser, Roles.Administrators)
                    .ConfigureAwaitFalse();
                }

                var result = new SelfViewModel {
                    Id = identityUser.Id, Email = identityUser.Email
                };

                return(new DataResponse <SelfViewModel>(result, null));
            }
            else
            {
                return(new DataResponse <SelfViewModel>(
                           $"{model.Email}", System.Net.HttpStatusCode.Unauthorized, null));
            }
        }