Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only allow registrations from specific email domains

I am developing a site that uses the built in account model / controller that comes with the new MVC site template. I want to be able to only allow people to register if they use one of two specific domains in their email address.

So for example they can register if they use @domain1.co.uk or @domain2.co.uk, but no other domains (for example Gmail, Yahoo etc) can be used.

If anyone could point me in the right direction that would be great.

like image 819
0NLY777 Avatar asked Apr 05 '11 15:04

0NLY777


2 Answers

What more do you need than:

if( email.Contains("@domain1.co.uk") || email.Contains("@domain2.co.uk") )
     Register(email);
else
    throw, return false, whatever()
like image 36
John Farrell Avatar answered Oct 27 '22 01:10

John Farrell


If using the MVC3 default site, you'll have a /Models/AccountModels.cs file. You can add a regular expression there to cause client-side* and server-side validation.

public class RegisterModel
{
    ...

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    [RegularExpression(@"^[a-zA-Z0-9._%+-]+(@domain1\.co\.uk|@domain2\.co\.uk)$", ErrorMessage = "Registration limited to domain1 and domain2.")]
    public string Email { get; set; }

    ...
}

You will need to work out the expression that works out best for your requirements.

*client-side validation assumes your view references the jquery.validate script and has Html.ValidationMessageFor(m => m.Email) and/or Html.ValidationSummary(), which it should by default.

like image 141
JustinStolle Avatar answered Oct 26 '22 23:10

JustinStolle