Search code examples
asp.net-coreasp.net-core-identity

How does CanSignInAsync(xyz) method of SignInManager work?


In ASP.NET Core Identity, how does CanSignInAsync(xyz) method of SignInManager work, what's it's real purpose, and what methods of the custom store provider interfaces (e.g. IUserStore) implementations does it hit?


Solution

  • From source code of CanSignInAsync(TUser) Method, we can find this method can help check if the specified user can sign in based on confirmation status, like below.

    public virtual async Task<bool> CanSignInAsync(TUser user)
    {
        if (Options.SignIn.RequireConfirmedEmail && !(await UserManager.IsEmailConfirmedAsync(user)))
        {
            Logger.LogWarning(0, "User {userId} cannot sign in without a confirmed email.", await UserManager.GetUserIdAsync(user));
            return false;
        }
        if (Options.SignIn.RequireConfirmedPhoneNumber && !(await UserManager.IsPhoneNumberConfirmedAsync(user)))
        {
            Logger.LogWarning(1, "User {userId} cannot sign in without a confirmed phone number.", await UserManager.GetUserIdAsync(user));
            return false;
        }
        if (Options.SignIn.RequireConfirmedAccount && !(await _confirmation.IsConfirmedAsync(UserManager, user)))
        {
            Logger.LogWarning(4, "User {userId} cannot sign in without a confirmed account.", await UserManager.GetUserIdAsync(user));
            return false;
        }
        return true;
    }