Identity email with dash in Asp.Net Identity

I am using ASP.NET MVC identity and when I add an email of the form [email protected] I get the validation error message

User name [email protected] 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);
                }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x