Question

I am teaching myself Asp .net MVC 3. I have reached a stage where I need to now incorporate user account system. I started with standard Asp .net MVC 3 membership provider and want to stick with it for most part and store all other user details in another table linked to users table.

However, I would like to add two features at the time of registration: 1) Confirmation Email 2) Accept T&C.

Is there an easy way to do this without building a custom membership provider?

Was it helpful?

Solution

Personally, I don't believe confirmations emails/terms and conditions should be part of a membership provider.

You normally would deal with things like that in your AccountController (or whatever equivalent you have).

For instance, if you have a Register action, once you've used the membership provider to save a user and everything looks good, you would then send out the confirmation email (code being triggered from the action)

OTHER TIPS

You can add the accept terms and conditions to the RegisterModel class in the Account Model (AccountModels.cs in the Models directory), like this:

[Display(Name = "Agree to Terms")]
public bool AgreeTandC { get; set; }

You have to update the Register view, adding the display for this so that a Check Box will be displayed and whatever text you want with it.

Unfortunately you cannot simply use the [Required] attribute since this is a bool and not being checked is false not null. There are ways to create a custom bool required attribute but they are a bit of work. It may be easier for you to add a check in the Register method in the Account Controller to see if it is checked, something like this:

if(model.AgreeTandC == false)
{
    ModelState.AddModelError("AgreeTandC", "You must agree to the Terms and Conditions");
    return View(model);
}

I haven't used the ModelState.AddModelError like that before but I think it will work (display the error to the user) otherwise just replace it with ViewBag.Error = "You must agree to the Terms and Conditions"; and add this to the register view model.

As for the email, it's not hard, you have to add the ability to send email something like MvcMailer will help with that. You also need to change the Membership.CreateUser line in the Register method of the Account Controller to not authorize the user.

Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, false, null, out createStatus);

I think that is how it should be. Then if the createStatus == MembershipCreateStatus.Success) send the email with the confirmation GUID string confirmationGuid = user.ProviderUserKey.ToString();

You will also have to make a method to accept the confirmation ID (when they click the link in their email.)

I just briefly touched on the email part, there are many blogs online that go into sending registration emails in complete detail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top