具有特殊字符的ASP.NET MVC身份电子邮件/用户名 [英] ASP.NET MVC Identity Email/Username with Special Characters

查看:82
本文介绍了具有特殊字符的ASP.NET MVC身份电子邮件/用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当通过 Web API 用诸如"xxx-yyy@gmail.com"之类的电子邮件注册帐户时,Fiddler返回跟随错误. 请注意,电子邮件也用于用户名,因此两个字段都相同.但是当在MVC本身上注册时,它可以工作.

When registering an account through Web API with an email such as "xxx-yyy@gmail.com", Fiddler returns the follow error. Note that email is used for username as well, so both fields are the same. But it works when registering on MVC itself.

ExceptionMessage =用户创建失败-身份异常.错误是:

ExceptionMessage=User Creation Failed - Identity Exception. Errors were:

用户名xx-yyy@gmail.com无效,只能包含字母或数字.

User name xx-yyy@gmail.com is invalid, can only contain letters or digits.

用户对象

 var newUser = new ApplicationUser {
            UserName = user.Email,
            Email = user.Email
        };

IdentifyConfig.cs

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<ApplicationUser>(manager) {
            RequireUniqueEmail = true,
            AllowOnlyAlphanumericUserNames = false

        };

我尝试注释掉 AllowOnlyAlphanumericUserNames ,但是它不起作用.通过将其正确设置为false,应该允许使用特殊字符,在我的情况下为连字符(-).

I have tried commenting out AllowOnlyAlphanumericUserNames but it didn't work. By right setting it to false should allow special characters, in my case a hyphen(-).

API控制器

// POST: api/auth/register
    [ActionName("Register")]
    public async Task<HttpResponseMessage> PostRegister(Auth user) {


        //dash issue is here. 

        var userContext = new ApplicationDbContext();

        var userStore = new UserStore<ApplicationUser>(userContext);

        var userManager = new UserManager<ApplicationUser>(userStore);

        var newUser = new ApplicationUser {
            UserName = user.Email,
            Email = user.Email
        };

        var result = await userManager.CreateAsync(newUser, user.PasswordHash);

        if (result.Succeeded) {
        ...

解决方案

IdentityConfig.cs 没有任何更改.唯一的变化是对我的API控制器.

There is NO changes to IdentityConfig.cs. Only changes is to my API Controller.

  // POST: api/auth/register
        [ActionName("Register")]
        public async Task<HttpResponseMessage> PostRegister(Auth user) {

//Changed to the following line
            ApplicationUserManager userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();

            var newUser = new ApplicationUser {
                UserName = user.Email,
                Email = user.Email
            };

            var result = await userManager.CreateAsync(newUser, user.PasswordHash);

推荐答案

您正在ApplicationUserManagerCreate方法中设置AllowOnlyAlphanumericUserNames = false. 在ApiControllerPostRegister动作中,您正在新建ApplicationUserManager的实例. AllowOnlyAlphanumericUserNames默认为true.

You are setting AllowOnlyAlphanumericUserNames = false in Create method of ApplicationUserManager. Inside the PostRegister action of your ApiController you are newing an instance of ApplicationUserManager. AllowOnlyAlphanumericUserNames is true by default.

您可以更改ApplicationUserManager

public ApplicationUserManager(IUserStore<ApplicationUser> store): base(store)
{
      UserValidator = new UserValidator<ApplicationUser>(this)
      {
           AllowOnlyAlphanumericUserNames = false,
           RequireUniqueEmail = true
      };
}

这样,当您新建ApplicationUserManger的实例时,AllowOnlyAlphanumericUserNames会设置为false.

That way AllowOnlyAlphanumericUserNames is set to false when you new an instance of ApplicationUserManger.

注意:OwinContext获取ApplicationUserManager最好与在默认MVC5模板的AccountController中进行操作相同,或者使用依赖注入.

Note: Its better to get the ApplicationUserManager from OwinContext same as done in AccountController of default MVC5 template or use Dependency Injection.

这篇关于具有特殊字符的ASP.NET MVC身份电子邮件/用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆