在ASP.NET MVC划线标识的电子邮件 [英] Identity email with dash in ASP.NET MVC

查看:181
本文介绍了在ASP.NET MVC划线标识的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC的身份,当我添加表单test-name@testing.com的电子邮件,我得到验证错误消息
用户名test-name@testing.com是无效的,只能包含字母或数字。

我怎样才能更改验证,以便它将使邮件被接受?

我的CSHTML是:

  @using(Html.BeginForm(登录,帐户,新的{RETURNURL = ViewBag.ReturnUrl},FormMethod.Post,新{角色=形式}))
        {
            @ Html.AntiForgeryToken()            < D​​IV CLASS =REG-头>
                < H2>注册< / H>
            < / DIV>
            <&字段集GT;
                @if(ViewBag.IsRegister)
                {
                    @ Html.ValidationSummary(真,新{@class =TEXT-危险})
                }
                < D​​IV CLASS =保证金底-20>
                    @ Html.LabelFor(M = GT; m.RegisterViewModel.FirstName)LT; BR />
                    < D​​IV CLASS =输入组>
                        <跨度类=输入组插件>< I类=发发用户>< / I>< / SPAN>
                        @ Html.TextBoxFor(M = GT; m.RegisterViewModel.FirstName,新{@class =表格控})
                    < / DIV>
                    @ Html.ValidationMessageFor(M = GT; m.RegisterViewModel.FirstName,新{@class =TEXT-危险})
                < / DIV>                < D​​IV CLASS =保证金底-20>
                    @ Html.LabelFor(M = GT; m.RegisterViewModel.LastName)LT; BR />
                    < D​​IV CLASS =输入组>
                        <跨度类=输入组插件>< I类=发发用户>< / I>< / SPAN>
                        @ Html.TextBoxFor(M = GT; m.RegisterViewModel.LastName,新{@class =表格控})
                    < / DIV>
                    @ Html.ValidationMessageFor(M = GT; m.RegisterViewModel.LastName,新{@class =TEXT-危险})
                < / DIV>                < D​​IV CLASS =保证金底-20>
                    @ Html.LabelFor(M = GT; m.RegisterViewModel.Email)LT; BR />
                    < D​​IV CLASS =输入组>
                        <跨度类=输入组插件>< I类=发发用户>< / I>< / SPAN>
                        @ Html.TextBoxFor(M = GT; m.RegisterViewModel.Email,新{@class =表格控})
                    < / DIV>
                    @ Html.ValidationMessageFor(M = GT; m.RegisterViewModel.Email,新{@class =TEXT-危险})
                < / DIV>                < D​​IV CLASS =保证金底-20>
                    @ Html.LabelFor(M = GT; m.RegisterViewModel.Password)LT; BR />
                    < D​​IV CLASS =输入组>
                        <跨度类=输入组插件>< I类=发发锁>< / I>< / SPAN>
                        @ Html.PasswordFor(M = GT; m.RegisterViewModel.Password,新{@class =表格控})                    < / DIV>
                    @ Html.ValidationMessageFor(M = GT; m.RegisterViewModel.Password,新{@class =TEXT-危险})
                < / DIV>                < D​​IV CLASS =保证金底-20>
                    @ Html.LabelFor(M = GT; m.RegisterViewModel.ConfirmPassword)LT; BR />
                    < D​​IV CLASS =输入组>
                        <跨度类=输入组插件>< I类=发发锁>< / I>< / SPAN>
                        @ Html.PasswordFor(M = GT; m.RegisterViewModel.ConfirmPassword,新{@class =表格控})
                        < D​​IV CLASS =清除>< / DIV>
                    < / DIV>
                    @ Html.ValidationMessageFor(M = GT; m.RegisterViewModel.ConfirmPassword,新{@class =TEXT-危险})
                < / DIV>
                < D​​IV CLASS =行>                    < D​​IV CLASS =COL-MD-12>
                        <按钮类=BTN-U型=提交名称=命令值=注册>注册< /按钮>
                    < / DIV>
                < / DIV>
            < /字段集>
        }

这是我的控制器:

  RegisterViewModel模型= login.RegisterViewModel;
                VAR用户=新ApplicationUser(){用户名= model.Email,名字= model.FirstName,姓氏= model.LastName,UserRole的= UserRole.Rider};
                IdentityResult结果=等待UserManager.CreateAsync(用户,model.Password);
                如果(result.Succeeded)
                {
                    等待SignInAsync(用户,isPersistent:假);                    //有关如何启用帐户确认和重置密码,请访问http://go.microsoft.com/fwlink/?LinkID=320771更多信息
                    //发送一封电子邮件,该链接
                    //字符串code =等待UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // VAR callbackUrl = Url.Action(ConfirmEmail,帐户,新的{用户id = user.Id,code = code},协议:Request.Url.Scheme);
                    //等待UserManager.SendEmailAsync(user.Id,确认您的账户,通过点击&LT请确认您的账户; A HREF = \\+ callbackUrl +\\>此处< / A>中);                    返回RedirectToLocal(RETURNURL);
                    //返回RedirectToAction(指数,家);
                }
                其他
                {
                    ViewBag.ReturnUrl = RETURNURL;
                    AddErrors(结果);
                }

编辑:谢谢你对这个援助

其实,我整理了这一点,通过把下面的帐户控制器

 公开的AccountController(的UserManager< ApplicationUser>的UserManager)
    {
        VAR提供商=新Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider(1);
        userManager.UserTokenProvider =新Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create(\"EmailConfirmation\"));
        的UserManager =的UserManager;        UserManager.UserValidator =新UserValidator&LT; ApplicationUser&GT;(的UserManager)
        {
            AllowOnlyAlphanumericUserNames = FALSE
        };
    }


解决方案
是在的UserManager 默认 UserValidator C>有一个属性 AllowOnlyAlphanumericUserNames 被设置为true ..你将需要重写此,让特殊字符。一个可能的解决办法是用自己的实现,像这样覆盖的UserManager。

 公共类ApplicationUserManager:&的UserManager LT; ApplicationUser&GT;
{
    公共ApplicationUserManager(IUserStore&LT; ApplicationUser&GT;存储):基地(存储)
    {
        this.UserValidator =新UserValidator&LT; ApplicationUser&GT;(本){AllowOnlyAlphanumericUserNames = FALSE};
    }
}公共类ApplicationUser:IdentityUser
{
    //自定义属性等。
}

I am using ASP.NET MVC identity and when I add an email of the form test-name@testing.com I get the validation error message "User name test-name@testing.com is invalid, can only contain letters or digits."

How can I change the validation so that it will allow the email to be accepted?

My cshtml is:

@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>
        }

This is my controller:

                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", "Account", 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);
                }

EDIT: Thanks for the assistance on this.

I actually sorted this out by putting the following in the account controller

        public AccountController(UserManager<ApplicationUser> userManager)
    {
        var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("One");
        userManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));
        UserManager = userManager;

        UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager)
        {
            AllowOnlyAlphanumericUserNames = false
        };
    }

解决方案

The default UserValidator that is used in the UserManager has a property AllowOnlyAlphanumericUserNames that is set to true.. you will need to override this to allow the special characters. A possible solution would be to override the UserManager with your own implementation like so.

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.
}

这篇关于在ASP.NET MVC划线标识的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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