首页 > 解决方案 > IdentityServer 4 使用用户名或电话号码登录

问题描述

在我们的产品中,我们让移动应用程序与针对身份验证的 API 进行交互 IdentityServer 4,业务设置要求能够使用同一个人的电话(SMS 身份验证)或用户名身份验证登录。我找到了这个示例项目,以便使用手机短信登录,效果很好: IdentityServer4.PhoneNumberAuth

我怎样才能结合用户名登录?

标签: c#authenticationoauth-2.0identityserver4openid

解决方案


创建一个继承自 IUserStore 的自定义 UserStore。请参考以下代码片段以使用用户名或电子邮件或电话号码登录。

public partial class UserStore : IUserStore<ApplicationUser>
{
    //Other method implementation 

    public Task<ApplicationUser> FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken)
    {
        cancellationToken.ThrowIfCancellationRequested();
        ThrowIfDisposed();

        if (normalizedUserName == null)
        {
            throw new ArgumentNullException(nameof(normalizedUserName));
        }

        return Task.FromResult(authDbContext.Users.FirstOrDefault(u => u.NormalizedUserName == normalizedUserName
        || u.PhoneNumber == normalizedUserName || u.NormalizedEmail == normalizedUserName));
    }
}

推荐阅读