配置Microsoft.AspNet.Identity以允许电子邮件地址作为用户名

Lia*_*mGu 119 asp.net asp.net-identity

我正在创建一个新的应用程序并开始使用EF6-rc1,Microsoft.AspNet.Identity.Core 1.0.0-rc1,Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc1,Microsoft.AspNet.Identity .Owin 1.0.0-rc1等昨天和RTM发布时,我今天晚上通过NuGet更新了它们到RTM.

除了我到目前为止所完成的工作的一些代码更改,所有似乎都进展顺利,直到我尝试为应用程序创建本地用户帐户.

我一直在处理电子邮件地址作为用户名格式,发布候选人工作得很好,但现在在创建一个用户名的电子邮件地址的用户时,它会引发以下验证错误:

User name xxxxx@xxxx.com is invalid, can only contain letters or digits.
Run Code Online (Sandbox Code Playgroud)

我花了大约一个小时左右的时间来搜索有关配置选项的解决方案或文档,但无济于事.

有没有办法可以配置它以允许用户名的电子邮件地址?

Hao*_*ung 161

你可以通过在UserManager上插入你自己的UserValidator,或者只是在默认实现上关闭它来允许这个:

UserManager.UserValidator = new UserValidator<TUser>(UserManager) { AllowOnlyAlphanumericUserNames = false }
Run Code Online (Sandbox Code Playgroud)

  • 在你的AccountController中,在`public AccountController(UserManager <ApplicationUser> userManager)`构造函数中添加`UserManager.UserValidator = new UserValidator <ApplicationUser>(UserManager){AllowOnlyAlphanumericUserNames = false};` (28认同)
  • 您将如何继续创建自定义UserValidator? (3认同)
  • 应该在代码中(在哪个文件/方法中)(在默认的mvc5应用程序中)应该放置UserManager.UserValidator = new UserValidator <TUser>(UserManager){AllowOnlyAlphanumericUserNames = false}?谢谢. (2认同)

小智 15

这个的C#版本(在App_Code\IdentityModels.cs中)是

public UserManager()
        : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
        UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
    }
Run Code Online (Sandbox Code Playgroud)


N1n*_*B0b 8

就我而言,使用ASP.NET Identity 2.0在VS 2013 C#,MVC 5.2.2中运行,解决方案是更新App_Start\IdentityConfig.cs中的ApplicationUserManager构造函数,如下所示:

public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    {
        this.UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
    }
Run Code Online (Sandbox Code Playgroud)


小智 8

我遇到了同样的问题,当我尝试更改使用户名是该人的真实姓名而不是电子邮件的代码时,系统向我显示相同的错误消息“用户名 ABC DEF 无效,只能包含字母或数字.” 我解决了向 AllowedUserNameCharacters 添加空格字符(就我而言是最后)的问题。

我使用的是 Asp.Net Core 2.2 和 VS2017

这是我的代码

转到 Startup.cs 并在“//user settings”下编辑或添加行:

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.Configure<IdentityOptions>(options =>
        {
            // Password settings.
            options.Password.RequireDigit = true;
            options.Password.RequireLowercase = true;
            options.Password.RequireNonAlphanumeric = true;
            options.Password.RequireUppercase = true;
            options.Password.RequiredLength = 6;
            options.Password.RequiredUniqueChars = 1;

            // Lockout settings.
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            options.Lockout.MaxFailedAccessAttempts = 5;
            options.Lockout.AllowedForNewUsers = true;


            // User settings.
            options.User.AllowedUserNameCharacters =
                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+ ";
            options.User.RequireUniqueEmail = false;
        });

        services.ConfigureApplicationCookie(options =>
Run Code Online (Sandbox Code Playgroud)


bsi*_*ma1 6

对于 AspNet.Identity.Core 2.1 及更高版本的用户,UserManager 中的这些验证器是只读的。默认情况下允许使用电子邮件地址作为用户名,但如果您需要进一步自定义用户名中的字符,您可以在 Startup.cs 中这样做:

public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentity<ApplicationUser, IdentityRole>(options => {
        options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+/";
    });

    // ... etc
}
Run Code Online (Sandbox Code Playgroud)

(出于遗留原因,我需要一个“/”。)