通过电话号码查找身份用户管理员

问题描述 投票:0回答:3

在aspnet core Identity2.0中,有没有办法通过电话号码查找用户?

UserManager
可以通过用户名和电子邮件查找用户,但无法通过电话查找,甚至更好,提供通用
find(Func<TUser, bool>)
功能。当用户使用电话注册时,需要检查给定的电话号码是否已经存在,因为一部电话不能被两个用户使用。

编辑

启动授权码:

services.AddIdentity<AccountUser, IdentityRole>()
            .AddEntityFrameworkStores<AccountDbContext>()
            .AddDefaultTokenProviders();
services.AddAuthentication().AddJwtBearer(...);
c# asp.net-core asp.net-identity
3个回答
1
投票

如果您想需要唯一的电话号码,您可以通过添加以下方法在

ApplicationDbContext
(或用作 IdentityDbContext 的任何上下文)中执行此操作:

protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
    if (entityEntry != null && entityEntry.State == EntityState.Added)
    {
        var errors = new List<DbValidationError>();
        var user = entityEntry.Entity as ApplicationUser;
        //check for uniqueness of phone number
        if (user != null)
        {
            if (Users.Any(u => String.Equals(u.PhoneNumber, user.PhoneNumber)))
            {
                errors.Add(new DbValidationError("User",user.PhoneNumber+" is already registered."));
            }
        }
    }
    return base.ValidateEntity(entityEntry, items); //check for uniqueness of user name and email and return result
}

继承的

base.ValidateEntity
方法用于检查以确保用户名和电子邮件是唯一的(如果您在配置中指定了这些选项)。

对于使用语句,您似乎需要:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

至于您提出的确切问题:要访问电话号码,您需要直接访问数据上下文。据我所知

UserManager
甚至
UserStore
都没有实现这样的方法,尽管您可以定义继承自
UserStore
的自己的用户存储类来添加这样的方法。


0
投票

使用此代码

UserStoreCustom.cs

 public class UserStoreCustom : UserStore<User>
    {
        public UserStoreCustom(ApplicationDbContext context, IdentityErrorDescriber describer = null)
            : base(context, describer)
        {

        }
        public virtual Task<User> FindByPhoneNumberAsync(string PhoneNumber, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            return Users.FirstOrDefaultAsync(u => u.PhoneNumber == PhoneNumber, cancellationToken);
        }

启动.cs

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(configuration.GetConnectionString("DataCenter")));
   
            services.AddIdentity<User, IdentityRole>(option =>
            {
                // configure identity options
                option.Password.RequireDigit = false;
                option.Password.RequireLowercase = false;
                option.Password.RequireUppercase = false;
                option.Password.RequireNonAlphanumeric = false;
                option.Password.RequiredLength = 4;
                option.SignIn.RequireConfirmedPhoneNumber = true;
            })

                .AddRoles<IdentityRole>()
                .AddRoleManager<RoleManager<IdentityRole>>()
                 .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddUserStore<UserStoreCustom>()
                .AddDefaultTokenProviders();

            services.AddSingleton<UserStoreCustom>();

0
投票

UserManager 类已经提供了实现此任务的便捷方法。您可以直接使用Where LINQ方法根据指定条件(例如电话号码)过滤用户。这可以通过调用通过创建 UserManager 类的实例公开的 Users 属性上的Where 方法来完成。

await _userManager.Users.Where(x=>x.PhoneNumber == "PhoneToCheck").FirstOrDefaultAsync();
© www.soinside.com 2019 - 2024. All rights reserved.