Reputation: 97
This is a more specific post of an earlier question. I have seen a number of posts with answers on this issue and none have worked for those who asked the question.
The question is, using .NET Identity, how does one use the username for validation on a web form (Not MVC) instead instead of the email address which is the default. I could post some code but honestly I am not exactly sure what class or method to post.
If you can answer this question kudos to you because no one else can figure it out.
Upvotes: 0
Views: 1107
Reputation: 97
After looking into things like creating a custom sign in manager (To do this with MVC see http://www.jamessturtevant.com/posts/ASPNET-Identity2.0-Custom-Database/) and viewing answers to this question for MVC ( See How to login with "UserName" instead of "Email" in MVC Identity?), it turns out that the answer is embarrassingly simple.
The Steps are as follows.
Modify Register.aspx and Login.aspx
On both pages create a UserName form control. Note that the field UserName already exists in the database so you do not have to add it. Just copy the current email form control and paste the copy back into the form. Then on the new form control change the following.
1. Change Email to UserName everywhere it appears in the new form control
2. Change ID="Email" to ID="UserName"
3. Change TextMode="Email" to TextMode="singleline"
4. Comment out the RequiredFieldValidator control that follows:
RequiredFieldValidator runat="server" ControlToValidate="Email"
CssClass="text-danger" ErrorMessage="The email field is required."
On Register.aspx.cs:
Change var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
To
var user = new ApplicationUser() { UserName = UserName.Text, Email = string.IsNullOrEmpty(Email.Text)? "[email protected]" : Email.Text }; (This is a bit of a hack but it works.)
On Login.aspx.cs
Change
var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
To
var result = signinManager.PasswordSignIn(UserName.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
On IdentityConfig.cs
Change
RequireUniqueEmail = true
To
RequireUniqueEmail = false
Upvotes: 1