Esempio n. 1
0
        public virtual async Task <SignInResult> PasswordSignInAsync(TUser user, string password,
                                                                     bool isPersistent, bool shouldLockout, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            var error = await PreSignInCheck(user, cancellationToken);

            if (error != null)
            {
                return(await LogResultAsync(error, user));
            }
            if (await IsLockedOut(user, cancellationToken))
            {
                return(await LogResultAsync(SignInResult.LockedOut, user));
            }
            if (await UserManager.CheckPasswordAsync(user, password, cancellationToken))
            {
                await ResetLockout(user, cancellationToken);

                return(await LogResultAsync(await SignInOrTwoFactorAsync(user, isPersistent, cancellationToken), user));
            }
            if (UserManager.SupportsUserLockout && shouldLockout)
            {
                // If lockout is requested, increment access failed count which might lock out the user
                await UserManager.AccessFailedAsync(user, cancellationToken);

                if (await UserManager.IsLockedOutAsync(user, cancellationToken))
                {
                    return(await LogResultAsync(SignInResult.LockedOut, user));
                }
            }
            return(await LogResultAsync(SignInResult.Failed, user));
        }
Esempio n. 2
0
        public virtual async Task <SignInStatus> PasswordSignInAsync(TUser user, string password,
                                                                     bool isPersistent, bool shouldLockout, CancellationToken cancellationToken = default(CancellationToken))
        {
            var error = await PreSignInCheck(user, cancellationToken);

            if (error != null)
            {
                return(error.Value);
            }
            if (await UserManager.CheckPasswordAsync(user, password, cancellationToken))
            {
                await ResetLockout(user, cancellationToken);

                return(await SignInOrTwoFactorAsync(user, isPersistent, cancellationToken));
            }
            if (UserManager.SupportsUserLockout && shouldLockout)
            {
                // If lockout is requested, increment access failed count which might lock out the user
                await UserManager.AccessFailedAsync(user, cancellationToken);

                if (await UserManager.IsLockedOutAsync(user, cancellationToken))
                {
                    return(SignInStatus.LockedOut);
                }
            }
            return(SignInStatus.Failure);
        }
Esempio n. 3
0
        public virtual async Task <SignInStatus> PasswordSignInAsync(string userName, string password,
                                                                     bool isPersistent, bool shouldLockout)
        {
            var user = await UserManager.FindByNameAsync(userName);

            if (user == null)
            {
                return(SignInStatus.Failure);
            }
            if (UserManager.SupportsUserLockout && await UserManager.IsLockedOutAsync(user))
            {
                return(SignInStatus.LockedOut);
            }
            if (await UserManager.CheckPasswordAsync(user, password))
            {
                return(await SignInOrTwoFactor(user, isPersistent));
            }
            if (UserManager.SupportsUserLockout && shouldLockout)
            {
                // If lockout is requested, increment access failed count which might lock out the user
                await UserManager.AccessFailedAsync(user);

                if (await UserManager.IsLockedOutAsync(user))
                {
                    return(SignInStatus.LockedOut);
                }
            }
            return(SignInStatus.Failure);
        }
Esempio n. 4
0
        public virtual async Task <SignInStatus> TwoFactorSignInAsync(string provider, string code, bool isPersistent)
        {
            var userId = await AuthenticationManager.RetrieveUserId();

            if (userId == null)
            {
                return(SignInStatus.Failure);
            }
            var user = await UserManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(SignInStatus.Failure);
            }
            if (await UserManager.IsLockedOutAsync(user))
            {
                return(SignInStatus.LockedOut);
            }
            if (await UserManager.VerifyTwoFactorTokenAsync(user, provider, code))
            {
                // When token is verified correctly, clear the access failed count used for lockout
                await UserManager.ResetAccessFailedCountAsync(user);
                await SignInAsync(user, isPersistent);

                return(SignInStatus.Success);
            }
            // If the token is incorrect, record the failure which also may cause the user to be locked out
            await UserManager.AccessFailedAsync(user);

            return(SignInStatus.Failure);
        }
 /// <summary>
 ///     Increments the access failed count for the user
 /// </summary>
 /// <param name="manager"></param>
 /// <param name="userId"></param>
 /// <returns></returns>
 public static IdentityResult AccessFailed <TUser, TKey>(this UserManager <TUser, TKey> manager, TKey userId)
     where TKey : IEquatable <TKey>
     where TUser : class, IUser <TKey>
 {
     if (manager == null)
     {
         throw new ArgumentNullException("manager");
     }
     return(AsyncHelper.RunSync(() => manager.AccessFailedAsync(userId)));
 }
Esempio n. 6
0
        public virtual async Task <SignInResult> TwoFactorSignInAsync(string provider, string code, bool isPersistent,
                                                                      bool rememberClient)
        {
            var twoFactorInfo = await RetrieveTwoFactorInfoAsync();

            if (twoFactorInfo == null || twoFactorInfo.UserId == null)
            {
                return(SignInResult.Failed);
            }
            var user = await UserManager.FindByIdAsync(twoFactorInfo.UserId);

            if (user == null)
            {
                return(SignInResult.Failed);
            }

            using (await BeginLoggingScopeAsync(user))
            {
                var error = await PreSignInCheck(user);

                if (error != null)
                {
                    return(Logger.Log(error));
                }
                if (await UserManager.VerifyTwoFactorTokenAsync(user, provider, code))
                {
                    // When token is verified correctly, clear the access failed count used for lockout
                    await ResetLockout(user);

                    // Cleanup external cookie
                    if (twoFactorInfo.LoginProvider != null)
                    {
                        Context.Response.SignOut(IdentityOptions.ExternalCookieAuthenticationScheme);
                    }
                    await SignInAsync(user, isPersistent, twoFactorInfo.LoginProvider);

                    if (rememberClient)
                    {
                        await RememberTwoFactorClientAsync(user);
                    }
                    await UserManager.ResetAccessFailedCountAsync(user);
                    await SignInAsync(user, isPersistent);

                    return(Logger.Log(SignInResult.Success));
                }
                // If the token is incorrect, record the failure which also may cause the user to be locked out
                await UserManager.AccessFailedAsync(user);

                return(Logger.Log(SignInResult.Failed));
            }
        }
Esempio n. 7
0
        public virtual async Task <SignInStatus> TwoFactorSignInAsync(string provider, string code, bool isPersistent,
                                                                      bool rememberClient, CancellationToken cancellationToken = default(CancellationToken))
        {
            var twoFactorInfo = await RetrieveTwoFactorInfoAsync(cancellationToken);

            if (twoFactorInfo == null || twoFactorInfo.UserId == null)
            {
                return(SignInStatus.Failure);
            }
            var user = await UserManager.FindByIdAsync(twoFactorInfo.UserId, cancellationToken);

            if (user == null)
            {
                return(SignInStatus.Failure);
            }
            var error = await PreSignInCheck(user, cancellationToken);

            if (error != null)
            {
                return(error.Value);
            }
            if (await UserManager.VerifyTwoFactorTokenAsync(user, provider, code, cancellationToken))
            {
                // When token is verified correctly, clear the access failed count used for lockout
                await ResetLockout(user, cancellationToken);

                // Cleanup external cookie
                if (twoFactorInfo.LoginProvider != null)
                {
                    Context.Response.SignOut(IdentityOptions.ExternalCookieAuthenticationType);
                }
                await SignInAsync(user, isPersistent, twoFactorInfo.LoginProvider, cancellationToken);

                if (rememberClient)
                {
                    await RememberTwoFactorClientAsync(user, cancellationToken);
                }
                return(SignInStatus.Success);
            }
            // If the token is incorrect, record the failure which also may cause the user to be locked out
            await UserManager.AccessFailedAsync(user, cancellationToken);

            return(SignInStatus.Failure);
        }
Esempio n. 8
0
        public virtual async Task <SignInResult> PasswordSignInAsync(TUser user, string password,
                                                                     bool isPersistent, bool shouldLockout)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            using (await BeginLoggingScopeAsync(user))
            {
                var error = await PreSignInCheck(user);

                if (error != null)
                {
                    return(Logger.Log(error));
                }
                if (await IsLockedOut(user))
                {
                    return(Logger.Log(SignInResult.LockedOut));
                }
                if (await UserManager.CheckPasswordAsync(user, password))
                {
                    await ResetLockout(user);

                    return(Logger.Log(await SignInOrTwoFactorAsync(user, isPersistent)));
                }
                if (UserManager.SupportsUserLockout && shouldLockout)
                {
                    // If lockout is requested, increment access failed count which might lock out the user
                    await UserManager.AccessFailedAsync(user);

                    if (await UserManager.IsLockedOutAsync(user))
                    {
                        return(Logger.Log(SignInResult.LockedOut));
                    }
                }
                return(Logger.Log(SignInResult.Failed));
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Attempts to sign in the specified <paramref name="user"/> and <paramref name="password"/> combination
        /// as an asynchronous operation.
        /// </summary>
        /// <param name="user">The user to sign in.</param>
        /// <param name="password">The password to attempt to sign in with.</param>
        /// <param name="isPersistent">Flag indicating whether the sign-in cookie should persist after the browser is closed.</param>
        /// <param name="lockoutOnFailure">Flag indicating if the user account should be locked if the sign in fails.</param>
        /// <returns>The task object representing the asynchronous operation containing the <see name="SignInResult"/>
        /// for the sign-in attempt.</returns>
        public virtual async Task <SignInResult> PasswordSignInAsync(TUser user, string password,
                                                                     bool isPersistent, bool lockoutOnFailure)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var error = await PreSignInCheck(user);

            if (error != null)
            {
                return(error);
            }
            if (await IsLockedOut(user))
            {
                return(await LockedOut(user));
            }
            if (await UserManager.CheckPasswordAsync(user, password))
            {
                await ResetLockout(user);

                return(await SignInOrTwoFactorAsync(user, isPersistent));
            }
            Logger.LogWarning("User {userId} failed to provide the correct password.", await UserManager.GetUserIdAsync(user));

            if (UserManager.SupportsUserLockout && lockoutOnFailure)
            {
                // If lockout is requested, increment access failed count which might lock out the user
                await UserManager.AccessFailedAsync(user);

                if (await UserManager.IsLockedOutAsync(user))
                {
                    return(await LockedOut(user));
                }
            }
            return(SignInResult.Failed);
        }