Search code examples
asp.netvalidationemailmembershipwhitelist

ASP.Net - Register users based on e-mail whitelist


I'm looking for a solution to this, but cannot find one on the web. I've done this before in other languages, but I'm not sure how to go about this in ASP.Net. I'm using Visual Studio 2010 Express.

I'm currently thinking that I should put the Whitelist commands in the CreatingUser event in the RegisterUser control. To this, I get the user's supplied e-mail address, check to make sure the domain is in a separate table of whitelisted e-mail addresses. Continue registering the user if it's in the whitelist, fail if not.

Basically, the user MUST belong to a whitelisted e-mail address to join the website.

Could anyone guide me on how to do this specifically with ASP.Net? I'm building off of the basic project template created from ASP.Net, so this is all stock Microsoft code I'm working with. Code is great, but I'd like to actually learn what I'm doing too!

EDIT: I did create the following table in my ASPNETDB.mdf

whitelistTable:

whitelistID, bigint autoincrement

whitelistDomain, nvarchar(256)


Solution

  • If you are referring to the CreateUserWizard.CreatingUser event, the CreateUserWizard control supports the Email field and should automatically add it if you set it up that way in the membership provider. (Disclaimer: I haven't actually used this myself).

    <asp:CreateUserWizard ID="RegisterUser" runat="server" OnCreatingUser="RegisterUser_CreatingUser" ... > 
    ...
    </asp:CreateUserWizard>
    

    In the RegisterUser_CreatingUser handler:

    protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e) 
    { 
        if (!EmailIsInWhiteList(RegisterUser.Email))
        { 
            // Optional: display validation message. 
            e.Cancel = true; 
            return; 
        } 
    
        e.Cancel = false; 
    }