身份电子邮件与破折号在Asp.Net身份

我正在使用ASP.NET MVC身份,当我添加一个电子邮件形式test-name@testing.com我得到验证错误消息

User name test-name@testing.com is invalid,can only contain letters
or digits.

如何更改验证,以便允许接受电子邮件

我的cshtml是:

@using (Html.BeginForm("Login","Account",new { ReturnUrl = ViewBag.ReturnUrl },FormMethod.Post,new { role = "form" }))
        {
            @Html.AntiForgeryToken()

            <div class="reg-header">
                <h2>Register</h2>
            </div>
            <fieldset>
                @if (ViewBag.IsRegister)
                {
                    @Html.ValidationSummary(true,"",new { @class = "text-danger" })
                }
                <div class="margin-bottom-20">
                    @Html.LabelFor(m => m.Registerviewmodel.FirstName)<br />
                    <div class="input-group">
                        <span class="input-group-addon"><i class="fa fa-user"></i></span>
                        @Html.TextBoxFor(m => m.Registerviewmodel.FirstName,new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(m => m.Registerviewmodel.FirstName,new { @class = "text-danger" })
                </div>

                <div class="margin-bottom-20">
                    @Html.LabelFor(m => m.Registerviewmodel.LastName)<br />
                    <div class="input-group">
                        <span class="input-group-addon"><i class="fa fa-user"></i></span>
                        @Html.TextBoxFor(m => m.Registerviewmodel.LastName,new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(m => m.Registerviewmodel.LastName,new { @class = "text-danger" })
                </div>

                <div class="margin-bottom-20">
                    @Html.LabelFor(m => m.Registerviewmodel.Email)<br />
                    <div class="input-group">
                        <span class="input-group-addon"><i class="fa fa-user"></i></span>
                        @Html.TextBoxFor(m => m.Registerviewmodel.Email,new { @class = "form-control" })
                    </div>
                    @Html.ValidationMessageFor(m => m.Registerviewmodel.Email,new { @class = "text-danger" })
                </div>

                <div class="margin-bottom-20">
                    @Html.LabelFor(m => m.Registerviewmodel.Password)<br />
                    <div class="input-group ">
                        <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                        @Html.PasswordFor(m => m.Registerviewmodel.Password,new { @class = "form-control" })

                    </div>
                    @Html.ValidationMessageFor(m => m.Registerviewmodel.Password,new { @class = "text-danger" })
                </div>

                <div class="margin-bottom-20">
                    @Html.LabelFor(m => m.Registerviewmodel.ConfirmPassword)<br />
                    <div class="input-group">
                        <span class="input-group-addon"><i class="fa fa-lock"></i></span>
                        @Html.PasswordFor(m => m.Registerviewmodel.ConfirmPassword,new { @class = "form-control" })
                        <div class="clear"></div>
                    </div>
                    @Html.ValidationMessageFor(m => m.Registerviewmodel.ConfirmPassword,new { @class = "text-danger" })
                </div>
                <div class="row">

                    <div class="col-md-12">
                        <button class="btn-u" type="submit" name="command" value="Register">Register</button>
                    </div>
                </div>
            </fieldset>
        }

这是我的控制器

Registerviewmodel model = login.Registerviewmodel;
                var user = new ApplicationUser() { UserName = model.Email,FirstName = model.FirstName,LastName = model.LastName,UserRole = UserRole.Rider };
                IdentityResult result = await UserManager.CreateAsync(user,model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user,isPersistent: false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail",new { userId = user.Id,code = code },protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id,"Confirm your account","Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToLocal(returnUrl);
                    //return RedirectToAction("Index","Home");
                }
                else
                {
                    ViewBag.ReturnUrl = returnUrl;
                    AddErrors(result);
                }

解决方法

UserManager中使用的认UserValidator具有设置为true的属性AllowOnlyAlphanumericUserNames.您将需要覆盖此值以允许特殊字符.一个可能的解决方案是用你自己的实现覆盖UserManager,如此.
public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store)
    {
        this.UserValidator = new UserValidator<ApplicationUser>(this) { AllowOnlyAlphanumericUserNames = false };
    }
}

public class ApplicationUser : IdentityUser
{
    //Custom Properties etc.
}

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...