Search code examples
c#asp.net-mvcasp.net-mvc-4membership-providersimplemembership

Is there a way to use EMail address as the Username?


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?


Solution

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