身份用户经理通过phonenumber查找

时间:2017-11-24 08:38:29

标签: c# asp.net-core asp.net-identity

在aspnet core identity2.0中,无论如何通过其电话号码找到用户? UserManager可以通过用户名和电子邮件找到用户,但无法通过电话查找,甚至更好,提供了通用的find(Func<TUser, bool>)功能。当用户在手机上注册时,需要检查一个给定的电话号码是否已经存在,因为两个用户无法使用手机。

修改

启动身份验证码:

services.AddIdentity<AccountUser, IdentityRole>()
            .AddEntityFrameworkStores<AccountDbContext>()
            .AddDefaultTokenProviders();
services.AddAuthentication().AddJwtBearer(...);

2 个答案:

答案 0 :(得分: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);
        }

Startup.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>();

答案 1 :(得分:-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方法用于检查以确保UserName和Email是唯一的(如果您已在配置中指定了这些选项)。

对于使用语句,您看起来需要:

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的用户存储类来添加这样的方法。