Neo
Neo

Reputation: 16219

Custom form authentication MVC?

I have developed MVC web app. Now, I want to use custom form authentication into it. For simplicity I'm using SQL membership provider for registration stuff. But, I dont want to create other custom table and map membership table to that because I just need to create simple module i need to stick with default tables

I have generated sql membership table using Aspnet_regsql tool

Now in AccountModel.cs into RegisterModel class i have added two more fields like :

public class RegisterModel
{
   [Required]
   [Display(Name = "User name")]
   public string UserName { get; set; }

   [Required]
   [Display(Name = "First name")]
   public string FirstName { get; set; }

   [Required]
   [Display(Name = "Last name")]
   public string LastName { get; set; }
}

Also updated dbo.aspnet_Membership table and added two more fields firstname and lastname

Now inside Controllers in AccountController.cs code :

[HttpPost]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus;
        Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus); 
    }
}

Here as CreateUser is default method of membership so I'm not able to insert my custom fields (firstname and lastname )into table at the time of create user ?

How can I do this?

Upvotes: 0

Views: 1161

Answers (2)

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13584

Ashu, after reading your requirements, i feel you should go with coding a custom provider for yourself rather than sticking with the default. The reason I say this is that the only option you have on-hand to achieve your requirements is by following @Mystere Man's solution.

create a Users table in your app, make it's primary key a GUID, and add your custom fields. When you create the user, insert a new record into your users table and use the ProviderUserKey of the MembershipUser returned from CreateUser. This is extremely simple, and does not require messing with the membership system.

Now, if you are going to play with the membership provider, than why not make an entirely new provider for your own. It isn't that difficult as people think it is. All you need to do is implement some abstract classes to have things accordingly.

There are tons of tutorial available online for building a custom membership provider from scratch and i am listing few them below.

Upvotes: 1

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

Don't extend the membership tables. Just don't do it. Don't. I meant it. Don't do it. Really. I'm serious. Don't do it.

Instead, create a Users table in your app, make it's primary key a GUID, and add your custom fields. When you create the user, insert a new record into your users table and use the ProviderUserKey of the MembershipUser returned from CreateUser. This is extremely simple, and does not require messing with the membership system.

I know you said you don't want to, but that's the only good way to do it.

The only other alternative is to use the Profile provider to save this information, however you won't be able to map it to anything else. The Profile provider is pretty generic and most people find it to be not that useful.

If you modify the membership tables, you will break things. And there's no good way to deal with the data, as you've found out, without creating your own custom provider.

Upvotes: 2

Related Questions