Reputation: 187
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
Upvotes: 1
Views: 1668
Reputation: 1
var profile = ProfileBase.Create(userName);
var firstName = profile["FirstName"] as string;
var lastName = profile["LastName"] as string;
Where did you put this code?
Upvotes: 0
Reputation: 7605
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!
Upvotes: 2
Reputation: 8645
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.
Upvotes: 1
Reputation: 28970
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
Upvotes: 1