c# - IdentityServer 4 使用用户名或电话号码登录
问题描述
在我们的产品中,我们让移动应用程序与针对身份验证的 API 进行交互 IdentityServer 4
,业务设置要求能够使用同一个人的电话(SMS 身份验证)或用户名身份验证登录。我找到了这个示例项目,以便使用手机短信登录,效果很好:
IdentityServer4.PhoneNumberAuth
我怎样才能结合用户名登录?
解决方案
创建一个继承自 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));
}
}
推荐阅读
- javascript - 如何检查用户是否 idel 不使用我网页中的键盘和鼠标
- microsoft-graph-toolkit - Microsoft Graph Toolkit - 只是尝试加载而没有错误
- json - D3 中的链接工具提示
- tensorflow - Keras model.fit_generator 这个方法在哪里获取标签
- python - 为什么 nextds 宏在 subdags 中被错误地扩展?
- google-apps-script - 在我的函数中改变什么来改变它将归档的范围?
- python - Python 时间戳和事件创建
- button - 单击按钮后在散景图中加载数据源
- android - 将现有文件夹从外部存储移动到内部存储 API 30 Android
- c# - 如何修复使用性能分析结果发现的高 CPU 使用率?