Login with either email or username and ignore upper/lower cases for UserName during login

asp.net-identity asp.net-mvc asp.net-mvc-5 authentication entity-framework-6

Question

I'm working with EntityFramework 6 in an Asp.Net MVC5 web application. I changed the AspNetUser database table's UserName property so that it is a user-chosen value rather than an email address.

But this lesson says: (Separating the user name and email in ASP.NET Identity 2.0.0) "Despite the fact that the login page requests an email address, it really utilises a model. To verify the supplied email, use the UserName ". Why, therefore, does it demand the UserName even if it claims to be utilising the Email property, and how/where can I alter that rule?

What's more, how can I get the login to accept either an email address or a username, as it does on Facebook, and disregard capital or lowercase letters?

edited, with my code modifications:

This attribute was introduced to the RegisterViewModel.

[Required]
[Display(Name = "UserName")]
public string UserName { get; set; }

This code was added to the Register view:

<!-- (...) -->
<div class="form-group">
@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
    @Html.TextBoxFor(m => m.UserName, new { @class = "form-control " })
</div>
</div>
<!-- (...) -->

Changed the Register action function so that it now creates new users using the UserName as well:

// (...)
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
// (...)

changed the LoginViewModel's Email property to:

[Required]
[Display(Name = "User Name")]
public string UserName { get; set; }

Additionally, I changed the Email in the login window to UserName:

<!-- (...) -->
            <div class="form-group">
                @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
                </div>
            </div>
            <!-- (...) -->

Additionally, the Login action method was modified to utilise the UserName rather than the Email for login:

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
    return View(model);
}

// This doesn't count login failures towards lockout only two factor authentication
// To enable password failures to trigger lockout, change to shouldLockout: true
var result = await SignInHelper.PasswordSignIn(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
// (...)
1
2
7/31/2016 6:22:39 PM

Accepted Answer

ZZZ_tmp
2
7/31/2016 8:03:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow