0

I'm using the simple membership provider inside an asp.net mvc 4 app. I'm quite new and am just learning the asp.net mvc platform. I was wondering if there is a simple way to have visitors use their email address as their username?

Is it as simple as changing the label to email and doing a validation on the input to make sure it is an email address?

Is there a proper way of doing this?

Yusubov
  • 5,815
  • 9
  • 32
  • 69
hjavaher
  • 2,589
  • 3
  • 30
  • 52
  • Why would you want to use the email address as the username.. are you going to have a way to tie that email address to the actual user something like how yahoo does when you log onto check your email..? you would need to validate email address along with the user password for example.. – MethodMan Jan 08 '13 at 01:31
  • @Gordon I don't have any code written yet (other than whats there form the template) – hjavaher Jan 08 '13 at 01:53
  • @DJKRAZE Like I said, I'm just learning the mvc and there are a lot of services that use the email address as the customer's username to log in. I would like to achieve the same usability as Facebook for example were users can login with their email address. – hjavaher Jan 08 '13 at 01:57
  • Well perhaps you should do a google search and see how facebook does it.. – MethodMan Jan 08 '13 at 01:59

2 Answers2

2

There's no rules to what the username should be inside the membership API. You can simply use an email address to populate both the email address field and the username field. I have done it a few times when requirements called for it.

Also, you can use the DataType.EmailAddress enumeration to identify the property as an email address in your ViewModel and the validation framework will handle the check for you.

using System.ComponentModel.DataAnnotations;

public class AccountCreateViewModel
{
    [DataType(DataType.EmailAddress)]
    public string UserName { get; set; }
    /* ... */
}

Hope this helps.

andymeadows
  • 1,296
  • 8
  • 14
0

You can keep the username as a string and use this Regular Expression to validate it:

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
Allan Spreys
  • 5,287
  • 5
  • 39
  • 44
  • Thank you, I was just wondering if there is a more proper way of doing this but I guess not. – hjavaher Jan 08 '13 at 02:27
  • Have a look at the , HTML5 input element more info @(http://www.w3schools.com/html/html5_form_input_types.asp). You still have to implement fall backs as not every browser supports this yet at this time. – Syneryx Jan 08 '13 at 08:31