c# - .NET MVC 6/vNext UserValidator 允许字母数字字符

标签 c# asp.net .net asp.net-mvc asp.net-core

我想做的只是允许在 .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/

相关文章:

c# - 我可以知道在 Visual Studio 2015 中调试期间执行的最后一条语句吗?

asp.net - 回发或回调参数无效。调试问题

c# - 解析 JSON 对象数组

java - .NET 和 Java 之间的对称加密

c# - 如何在 C# 中获取和设置环境变量?

c# - 是否可以将 ASP.NET 成员资格与在 ASP.NET Identity 2.0 中创建的表一起使用以进行身份​​验证?

c# - 主窗口如何捕获其包含的用户控件中属性的 NotifyPropertyChanged 事件

C# 和 SQL Server 2008 CLR 序列化问题

javascript - 不能定位弹出div

c# - 如何在 WPF 中为我的弹出窗口应用图标?