Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#web应用程序中配置ASP.Net标识,以便使用电子邮件地址作为用户名_Asp.net_Identity - Fatal编程技术网

如何在C#web应用程序中配置ASP.Net标识,以便使用电子邮件地址作为用户名

如何在C#web应用程序中配置ASP.Net标识,以便使用电子邮件地址作为用户名,asp.net,identity,Asp.net,Identity,我正在从事一个使用ASP.Net Identity 2.0的项目。我的项目是基于ASP.NET4.5C#Web应用程序的 我想更改身份的配置,以便我可以使用电子邮件地址而不是用户名。但是,当我尝试注册一条记录时,错误向我显示,用户名只能使用字母数字 我在我的C#web应用程序中对IdentityModels.cs做了以下更改,但它不起作用: public UserManager() : base(new UserStore<ApplicationUser>(new Applic

我正在从事一个使用ASP.Net Identity 2.0的项目。我的项目是基于ASP.NET4.5C#Web应用程序的

我想更改身份的配置,以便我可以使用电子邮件地址而不是用户名。但是,当我尝试注册一条记录时,错误向我显示,用户名只能使用字母数字

我在我的C#web应用程序中对IdentityModels.cs做了以下更改,但它不起作用:

public UserManager()
   : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
        UserValidator = new UserValidator<ApplicationUser>(this) {
                AllowOnlyAlphanumericUserNames = false };
    }
公共用户管理器() :base(新的用户存储(新的ApplicationDbContext())) { UserValidator=新的UserValidator(此){ AllowOnlyAlphanumericUserNames=false}; } 我该怎么办


请帮帮我

首先更改代码如下。

   public UserManager()
   : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
        UserValidator = new UserValidator<ApplicationUser>(this) {
                AllowOnlyAlphanumericUserNames = true,
                RequireUniqueEmail = true };
    }

更改应用程序以支持该配置设置,以便轻松完成此工作,您无需进行任何数据库更改。

感谢您的解决方案!我在C#项目中检查了ASP.Net标识,发现它是1.0版(默认)。所以,我安装了ASP.NETIndetityVersion2.0,它确实对我有用。
// Configure the application user manager which is used in this application.
    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }
        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
            IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;

            // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
            // You can write your own provider and plug it in here.
            manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
            {
                MessageFormat = "Your security code is {0}"
            });
            manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
            {
                Subject = "Security Code",
                BodyFormat = "Your security code is {0}"
            });
            manager.EmailService = new EmailService();
            manager.SmsService = new SmsService();
            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider =
                    new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
    }
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);