1

I everyone,

I would like to add one or more field on my register page. I try to add my new field in argument to my Membership.CreateUser() method but it tells me it doesn't support more than 8 args. What can I do ? Develop my own custom provider ? There is no other solution, or trick ?

Excuse me for my english, and I begin in asp, so all you could tell me is well coming. Thanks

Alternative
  • 187
  • 2
  • 3
  • 13

4 Answers4

2

You don't have to create a custom provider in order to add fields to the User table (though it may be the proper solution, depending, it's not mandatory).

To add a field to the Users table, go to SQL Server Management Studio, find and expand aspnetdb. Expand Tables and right click on Users. Choose Design, add the fields you want and click save. That's it!

EDIT: Implementing Profiles


Okay, let's assume you want to add two Profile attributes to your users: FirstName and LastName, both strings (or nvarchar in db terms)

Web.Config (the name and type are defaults, you'll need to set your connection string for the aspnetdb)

<profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider"
             type="System.Web.Profile.SqlProfileProvider"
             connectionStringName="ApplicationServices"
             applicationName="/"/>
      </providers>
      <properties>
        <add name="FirstName" type="string"/>
        <add name="LastName" type="string"/>
      </properties>
    </profile>

So now that you have a profile provider configured and properties defined, you can work with them via the ProfileBase class. For example, when a new user registers at your site, in addition to the username and password that is required for Membership.CreateUser, you can ask your users to enter their first and last names. Once they've posted the form back to your server, you'd process it like so:

[HttpPost]
        public ActionResult CreateUser( ViewModel viewModel )
        {
            if (ModelState.IsValid)
            {
                    MembershipCreateStatus status;
                    var newUser = Membership.CreateUser( viewModel.UserName, viewModel.Password, viewModel.EmailAddress, null, null, true, null, out status );
                    if (status == MembershipCreateStatus.Success)
                    {
                        //set profile data
                        ProfileBase profile = ProfileBase.Create(viewModel.UserName);
                        profile["FirstName"] = viewModel.FirstName;
                        profile["LastName"] = viewModel.LastName;
                        profile.Save();
                    }

Editing Profile data is the same as creating it.

Getting Profile data for a user is just as easy:

var profile = ProfileBase.Create(userName);
var firstName = profile["FirstName"] as string;
var lastName = profile["LastName"] as string;

Hope this helps get you going!

Forty-Two
  • 7,535
  • 2
  • 37
  • 54
  • Adding the field is ok, but after I can't access it in my code with the createUser method... Thanks anyway – Alternative Aug 28 '12 at 14:13
  • Have you considered using profiles? They may be a simple solution – Forty-Two Aug 28 '12 at 14:39
  • You mean the table aspnet_Profile ? – Alternative Aug 29 '12 at 08:09
  • Hi, even having read msdn doc on Profiles I can't implement it. You tell me it will be a simple solution but I can't see how and where implement it in my code. I've create a new table, I've add fields in my AccountModels.cs, in my view too, but I don't know how access and update data in my new table from my AccountController. I'm desesperate ^^ I don't understand too what is the purpose of defining in web.config. It don't seems too hard yet but I'm really stuck – Alternative Aug 30 '12 at 13:49
  • @Alternative just updated my answer with a bit of detail on profiles and how to use them – Forty-Two Aug 30 '12 at 14:24
  • Thanks for you help and time. Do I need to modify applicationName="/" ? I wrote the four lignes ProfileBase... in my Post Method Register, is that good ? And I had my two new properties in my AccountModel too..But when I go to my Register View, my two fields appear well, but they hide content like it was a password when I wrote data, and when I submit the form, I've this error : The connection name 'ApplicationServices' was not found in the applications configuration or the connection string is empty. But my info are well updated in my Users and Membership tables, but not my two new properties – Alternative Aug 31 '12 at 09:03
  • So, I was keep looking, I add a new connectionString like that ` ` and it seems to work, my Profile table was updated, but my fields always hide content, but it's another issue. I guess I'm good now, thks – Alternative Aug 31 '12 at 09:18
  • Glad it's working. As far as 'hiding' the input, check that the properties are not decorated with [DataType(DataType.Password)] in your model. – Forty-Two Aug 31 '12 at 12:08
1

Exactly you must customize your member ship provider

i worked , you can read this article : http://www.codeproject.com/Articles/165159/Custom-Membership-Providers

he helps me in my work

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
1

I would suggest creating a new table, such as UserDetail, and creating your own queries for reading/writing from/to this table. So after calling Membership.CreateUser() if the user has been created, insert you custom data into your custom table.

You could create your own custom membership provider, but in my opinion this is more hassle than it's worth.

Jaimal Chohan
  • 8,530
  • 6
  • 43
  • 64
0
var profile = ProfileBase.Create(userName);
var firstName = profile["FirstName"] as string;
var lastName = profile["LastName"] as string;

Where did you put this code?