Matt Kagan
Matt Kagan

Reputation: 719

Membership.CreateUser for multiple Membership Providers

I am having trouble with handling multiple Membership Providers on one site.

I have a registration page that creates user and adds them to an AD domain. Before I had one membership provider and on the page I called

Membership.CreateUser(txtUsername.Text, txtPassword.Text) 

which created the username based on the user inputs on the page.

However, I added another membership provider to the web.config so that the log in page can be used to log in from another AD. Now, my registration page will not work because (I assume) there is two membership providers?

How can I fix this? How do I specify which provider to use when I call Membership.CreateUser?

Thanks.

Edit:

The correct way to do this, as mentioned below is :

MembershipCreateStatus newStatus = new MembershipCreateStatus();    
Membership.Providers["ProviderName"].CreateUser(txtUserName.Text, txtPassword.Text, null, null, null, true,null, out newStatus);

Thank you!

Upvotes: 4

Views: 3508

Answers (2)

gbs
gbs

Reputation: 7266

Membership.Providers["providername"].CreateUser(....);

Note that there is only one CreateUser method in this case but you can pass null of other parameters that are not needed.

Edit:

That is the only way to call that method with explicit provider. I haven't tried creating user in AD so not sure about resolving that error but here is another way. Make the provider for CreateUser as your defaultprovider and so you can go back to the old CreateUser call. But now you adjust your login call to use another provider e.g.

Membership.Providers["providername"].ValidateUser()

Upvotes: 9

Forgotten Semicolon
Forgotten Semicolon

Reputation: 14100

There is a Providers property on Membership that contains a collection of the membership providers. You should be able to discern which provider to use from there.

Upvotes: 1

Related Questions