Username and Email

Aug 8, 2014 at 8:35 AM
Edited Aug 8, 2014 at 9:16 AM
Hi,

I am building a .net MVC5 with EF6 app and I have hit a bit of a problem in the early stages which is quite frustrating as it is slowing down my progress.

Basically, I have a 'Register' section where I am allowing users to register for an individual account. The fields they have are - Email, Username, Password, Password Confirmation.

Now, when the user is registered, I would like a way to be able to authenticate the user via Email and password only. The site only requires the username when looking at the activity stream or the user profile. e.g. www.example.com/{username}

Is there a way to enable email and password authentication without having to hack at the core code.

I appreciate any advice or guidance.

Thank you in advance.

Mike
Aug 10, 2014 at 1:46 PM
I am making an assumption that you are writing code based on the 2013 template. You control the registration view, correct? Could you make the username field hidden, and set it to be the email address behind the scenes?
Aug 11, 2014 at 9:52 PM
Hi,

Thank you for the reply.

I am not using the 2013 template as I prefer to start with an empty solution and build up the application at a steady pace only including what I require.

As such, I am using the identity features however, rolling my own implementation taking into account the repository pattern and other useful architectures.

Anyways - I had a good google around and found a great article which explains exactly how to do what I wanted which was the following:
  • Users will register with username, email and password and they will store in the relevant fields in Identity users table.
  • Users can authenticate with either the username/password combo, or the email/password combo.
  • This will automatically be detected during the postback and the correct c# methods will be run depending on the input.
  • In order to do this, I have to create an extended method (based on UserManager.FindAsync(model.UserName, model.Password);) which will do the exact same thing except do a look up on email rather than username
This is the article in case any else would like to implement a similar feature.

http://anthonychu.ca/post/aspnet-identity-20---logging-in-with-email-or-username

Mike
Sep 6, 2014 at 4:13 PM
Edited Sep 8, 2014 at 4:45 PM
(comment removed)