CreateUserWizard - membership and roles

S

Sherman KB

I have the need to be able to create users and add them to a role. I'll also
need to associate the data with some other data. I'm using the sql provider,
and I'm looking for a way to wrap this data in a transaction. It looks like
the membership.create users either works for fails, and the roles.addusers
works the same. Ideally I'd like to hook both the membership.add and
roles.addusers into one atomic transaction. Is this possible?
 
P

PeterKellner

I have the need to be able to create users and add them to a role. I'll also
need to associate the data with some other data. I'm using the sql provider,
and I'm looking for a way to wrap this data in a transaction. It looks like
the membership.create users either works for fails, and the roles.addusers
works the same. Ideally I'd like to hook both the membership.add and
roles.addusers into one atomic transaction. Is this possible?

In this case, Transaction Scope's are your friend. You can read about
it here:

http://msdn2.microsoft.com/en-us/ms172152.aspx

Your code should look something like what I have below. (I even
tested it) With the throw in, the user doesn't get added even though
the CreateUser call is made successfully. (as I had hoped).

protected void Button1_Click(object sender, EventArgs e)
{
using (TransactionScope scope = new TransactionScope())
{
MembershipCreateStatus mstatus;
Membership.CreateUser("newuseryyy", "pass@word",
"(e-mail address removed)", "q", "a", true, out mstatus);

throw new ApplicationException("aborting, user should not
be added");

scope.Complete();

}

Good Luck
Peter Kellner
http://peterkellner.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top