.NET MVC 6/vNext UserValidator允许字母数字字符 [英] .NET MVC 6 / vNext UserValidator to allow alphanumeric characters
问题描述
我想做的只是在.NET MVC 6/vNext注册过程和我所看过的每个教程中允许字母数字字符(即
What I'm trying to do is simply allow alphanumeric characters in the .NET MVC 6 / vNext registration process and every tutorial I've seen (ie Configure Microsoft.AspNet.Identity to allow email address as username) references UserManager.UserValidator
, in which is now not available in the current framework.
我已经看到了: Microsoft.AspNet.Identity vnext中的UserValidator 看起来与我的问题相同,但是从那以后他们就把UserNameValidationRegex
属性从框架中删除了.很棒.
I've seen this: UserValidator in Microsoft.AspNet.Identity vnext which looks to be the same issue as mine, but they've taken the UserNameValidationRegex
property out of the framework since. Brilliant.
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
,但是由于找不到相关文档,我不知道该设置哪种格式?有没有人在同一条船上并且解决了这个问题?
I've found o.User.AllowedUserNameCharacters
, but with there being no documentation that I've found on this I've no idea what format I have to set that in? Anyone out there that's in the same boat and worked this out?
谢谢.
推荐答案
我看了 aspnet/身份GitHub存储库,并搜索了您找到的属性(AllowedUserNameCharacters
).这就是我所拥有的.
I took a look at the aspnet/Identity GitHub repository and did a search for the property you have found (AllowedUserNameCharacters
). Here's what I've got.
来自回购中的UserOptions.cs
类: [此处链接]
/// <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"
(即,从原始设置的字符串的末尾删除符号.这应该可以实现您想要的功能.
So it would seem that you will need to set this property to be "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
(i.e. remove the symbols from the end of the orginally set string. This should achieve what you want.
关于此更改的原因,根据问题#558,他们正试图摆脱正则表达式以提高安全性.在中此处.
In terms of why this change was made, according to issue #558, they are trying to move away from regular expressions for increased security. Have a look here for the full details.
这篇关于.NET MVC 6/vNext UserValidator允许字母数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!