我想做的只是允许在 .NET MVC 6/vNext 注册过程中使用字母数字字符,并且我看到的每个教程(即 Configure Microsoft.AspNet.Identity to allow email address as username)都引用 UserManager.UserValidator
,其中在当前框架中现在不可用。
我看过这个:UserValidator in Microsoft.AspNet.Identity vnext这看起来与我的问题相同,但从那以后他们就将 UserNameValidationRegex
属性从框架中移除了。太棒了。
services.AddIdentity<ApplicationUser, IdentityRole>(
o => {
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonLetterOrDigit = false;
o.Password.RequiredLength = 2;
//o.User.AllowedUserNameCharacters here seems to be the only logical thing I can access
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
我找到了 o.User.AllowedUserNameCharacters
,但是由于没有我找到的相关文档,我不知道我必须将其设置为什么格式?有没有人在同一条船上解决了这个问题?
提前致谢。
最佳答案
我查看了 aspnet/Identity GitHub repository 并搜索了您找到的属性 ( AllowedUserNameCharacters
)。这就是我得到的。
来自 repo 中的 UserOptions.cs
类:[Link Here]
/// <summary>
/// Gets or sets the list of allowed characters in the username used to validate user names.
/// </summary>
/// <value>
/// The list of allowed characters in the username used to validate user names.
/// </value>
public string AllowedUserNameCharacters { get; set; } = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
因此您似乎需要将此属性设置为 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
(即从原始设置的字符串末尾删除符号。这应该可以实现您想要的效果。
关于为什么要进行此更改,根据问题 #558,他们正试图摆脱正则表达式以提高安全性。查看 here 以获取完整详细信息。
关于c# - .NET MVC 6/vNext UserValidator 允许字母数字字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35221312/