How do I get User ID for new registered user in ASP.Net CreateUserWizard?
I have a page that allows new users to register with a Create User Wizard. Right after a user is created I would like to insert a row in a customer details table using the new user’s User ID and email.
I tried putting some code in the CreateUserWizard1_CreatedUser to get the User ID. But, from what I learned, the user is created at this point; but, the user is not logged in at this point. I get an error that says…
“NullReferenceException” was handled by user code.
Object not set to an instance of an object.”
If I put a breakpoint in during debugging I can see that the value of the customerId is null.
From what I read on the MSDN site, the CreatedUser event…
“Occurs after the membership provider has created the new Web site user account.”
I want to add the row to the customer details table before the Create User Wizard changes to display the “Continue” button. At that point, the user can go to other pages or close the application.
Is there some other event after the CreatedUser where I can put some code?
I see that there is an Unload event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.createuserwizard_events%28v=vs.110%29.aspx
Can I can somehow log in the user in the CreatedUser event to get the User ID?
Here is my code on the Register page that isn’t working. The error occurs on the line of code: “string customerId =”
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
// Add the Role of Customers to the new user.
Roles.AddUserToRole((sender as CreateUserWizard).UserName, "Customers");
// Get the current user's ID and email
string customerId = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString();
string email = Membership.GetUser(HttpContext.Current.User.Identity.Name).Email.ToString();
bool success = CustomerDetailsAccess.CreateCustomerDetails(customerId, email);
…
}
When a user is going through the registration process,
HttpContext.Current.User.Identitywould not be set (anonymous access) unless the user registering is already logged in.To solve the problem, you will need to get the username from the wizard instead of the context.
To fix the issue, change the following line:
to this: