protected virtual async Task SaveLoginAttempt(CodeZeroLoginResult <TTenant, TUser> loginResult, string tenancyName, string userNameOrEmailAddress)
        {
            using (var uow = UnitOfWorkManager.Begin(TransactionScopeOption.Suppress))
            {
                var tenantId = loginResult.Tenant != null ? loginResult.Tenant.Id : (int?)null;
                using (UnitOfWorkManager.Current.SetTenantId(tenantId))
                {
                    var loginAttempt = new UserLoginAttempt
                    {
                        TenantId    = tenantId,
                        TenancyName = tenancyName,

                        UserId = loginResult.User != null ? loginResult.User.Id : (long?)null,
                        UserNameOrEmailAddress = userNameOrEmailAddress,

                        Result = loginResult.Result,

                        BrowserInfo     = ClientInfoProvider.BrowserInfo,
                        ClientIpAddress = ClientInfoProvider.ClientIpAddress,
                        ClientName      = ClientInfoProvider.ComputerName,
                    };

                    await UserLoginAttemptRepository.InsertAsync(loginAttempt);

                    await UnitOfWorkManager.Current.SaveChangesAsync();

                    await uow.CompleteAsync();
                }
            }
        }
        public virtual async Task SignInAsync(CodeZeroLoginResult <TTenant, TUser> loginResult, bool isPersistent, bool?rememberBrowser = null)
        {
            if (loginResult.Result != CodeZeroLoginResultType.Success)
            {
                throw new ArgumentException("loginResult.Result should be success in order to sign in!");
            }

            using (_unitOfWorkManager.Current.SetTenantId(loginResult.Tenant?.Id))
            {
                await SignOutAllAsync();

                if (rememberBrowser == null)
                {
                    rememberBrowser = IsTrue(CodeZeroSettingNames.UserManagement.TwoFactorLogin.IsRememberBrowserEnabled, loginResult.Tenant?.Id);
                }

                if (rememberBrowser == true)
                {
                    await SignOutAllAndSignInAsync(loginResult.Identity, isPersistent);

                    await AuthenticationManager.SignInAsync(
                        _configuration.TwoFactorRememberBrowserAuthenticationScheme,
                        CreateIdentityForTwoFactor(
                            loginResult.User,
                            _configuration.TwoFactorRememberBrowserAuthenticationScheme
                            )
                        );
                }
                else
                {
                    await SignOutAllAndSignInAsync(loginResult.Identity, isPersistent);
                }
            }
        }
        public virtual async Task <SignInStatus> SignInOrTwoFactorAsync(CodeZeroLoginResult <TTenant, TUser> loginResult, bool isPersistent, bool?rememberBrowser = null)
        {
            if (loginResult.Result != CodeZeroLoginResultType.Success)
            {
                throw new ArgumentException("loginResult.Result should be success in order to sign in!");
            }

            using (_unitOfWorkManager.Current.SetTenantId(loginResult.Tenant?.Id))
            {
                if (IsTrue(CodeZeroSettingNames.UserManagement.TwoFactorLogin.IsEnabled, loginResult.Tenant?.Id))
                {
                    UserManager.As <CodeZeroUserManager <TRole, TUser> >().RegisterTwoFactorProviders(loginResult.Tenant?.Id);

                    if (await UserManager.GetTwoFactorEnabledAsync(loginResult.User.Id))
                    {
                        if ((await UserManager.GetValidTwoFactorProvidersAsync(loginResult.User.Id)).Count > 0)
                        {
                            if (!await TwoFactorBrowserRememberedAsync(loginResult.User.Id.ToString(), loginResult.User.TenantId) ||
                                rememberBrowser == false)
                            {
                                var claimsIdentity = new ClaimsIdentity(_configuration.TwoFactorAuthenticationScheme);

                                claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginResult.User.Id.ToString()));

                                if (loginResult.Tenant != null)
                                {
                                    claimsIdentity.AddClaim(new Claim(CodeZeroClaimTypes.TenantId, loginResult.Tenant.Id.ToString()));
                                }

                                await AuthenticationManager.SignInAsync(
                                    _configuration.TwoFactorAuthenticationScheme,
                                    CreateIdentityForTwoFactor(
                                        loginResult.User,
                                        _configuration.TwoFactorAuthenticationScheme
                                        )
                                    );

                                return(SignInStatus.RequiresVerification);
                            }
                        }
                    }
                }

                await SignInAsync(loginResult, isPersistent, rememberBrowser);

                return(SignInStatus.Success);
            }
        }
        public virtual void SignIn(CodeZeroLoginResult <TTenant, TUser> loginResult, bool isPersistent, bool?rememberBrowser = null)
        {
            if (loginResult.Result != CodeZeroLoginResultType.Success)
            {
                throw new ArgumentException("loginResult.Result should be success in order to sign in!");
            }

            using (_unitOfWorkManager.Current.SetTenantId(loginResult.Tenant?.Id))
            {
                AuthenticationManager.SignOut(
                    DefaultAuthenticationTypes.ExternalCookie,
                    DefaultAuthenticationTypes.TwoFactorCookie
                    );

                if (rememberBrowser == null)
                {
                    rememberBrowser = IsTrue(CodeZeroSettingNames.UserManagement.TwoFactorLogin.IsRememberBrowserEnabled, loginResult.Tenant?.Id);
                }

                if (rememberBrowser == true)
                {
                    var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(loginResult.User.Id.ToString());
                    AuthenticationManager.SignIn(
                        new AuthenticationProperties
                    {
                        IsPersistent = isPersistent
                    },
                        loginResult.Identity,
                        rememberBrowserIdentity
                        );
                }
                else
                {
                    AuthenticationManager.SignIn(
                        new AuthenticationProperties
                    {
                        IsPersistent = isPersistent
                    },
                        loginResult.Identity
                        );
                }
            }
        }
Exemplo n.º 5
0
        public virtual async Task <SignInResult> SignInOrTwoFactorAsync(CodeZeroLoginResult <TTenant, TUser> loginResult, bool isPersistent, bool?rememberBrowser = null, string loginProvider = null, bool bypassTwoFactor = false)
        {
            if (loginResult.Result != CodeZeroLoginResultType.Success)
            {
                throw new ArgumentException("loginResult.Result should be success in order to sign in!");
            }

            using (_unitOfWorkManager.Current.SetTenantId(loginResult.Tenant?.Id))
            {
                await UserManager.As <CodeZeroUserManager <TRole, TUser> >().InitializeOptionsAsync(loginResult.Tenant?.Id);

                if (!bypassTwoFactor && IsTrue(CodeZeroSettingNames.UserManagement.TwoFactorLogin.IsEnabled, loginResult.Tenant?.Id))
                {
                    if (await UserManager.GetTwoFactorEnabledAsync(loginResult.User))
                    {
                        if ((await UserManager.GetValidTwoFactorProvidersAsync(loginResult.User)).Count > 0)
                        {
                            if (!await IsTwoFactorClientRememberedAsync(loginResult.User) || rememberBrowser == false)
                            {
                                await Context.SignInAsync(
                                    IdentityConstants.TwoFactorUserIdScheme,
                                    StoreTwoFactorInfo(loginResult.User, loginProvider)
                                    );

                                return(SignInResult.TwoFactorRequired);
                            }
                        }
                    }
                }

                if (loginProvider != null)
                {
                    await Context.SignOutAsync(IdentityConstants.ExternalScheme);
                }

                await SignInAsync(loginResult.User, isPersistent, loginProvider);

                return(SignInResult.Success);
            }
        }