本文介绍了如何自定义ASP.NET Identity Core用户名以允许特殊字符和空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的注册操作方法更改为接受用户名而不是电子邮件.

I have changed my Register Action Method to accept user Name instead of Email.

if (ModelState.IsValid)
{
    var user = new ApplicationUser 
    {
        UserName = model.Name,
        Email = model.Email,
    };

但是当我在文本框中输入名称时,例如 Joe Smith .它给我一个错误,提示无效的用户名.它不允许用户名接受空白.我的问题是如何修改内置用户名以允许空格和特殊字符.我正在使用ASP.NET Core.

But when I Enter Name in TextBox like Joe Smith It give me an error that is Invalid UserName. It is not allowing user Name to Accept White space. My Question is how can I modify the builtin User Name to allow space and special characters. I am using ASP.NET Core.

预先感谢.

推荐答案

您可以在Startup.cs中设置用于配置身份的用户验证规则.

You can set user validation rules configuring identity with options in your Startup.cs.

services.AddIdentity<ApplicationUser, IdentityRole>(options => {
    options.User.AllowedUserNameCharacters = "allowed characters here";
    options.User.RequireUniqueEmail = true/false;
});

相关资源:

配置身份

这篇关于如何自定义ASP.NET Identity Core用户名以允许特殊字符和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:54