How to get user ID for newly registered user in ASP.Net CreateUserWizard?

How to get user ID for newly registered user in ASP.Net CreateUserWizard?

I have a page that allows new users to register using the create user wizard. Immediately after creating the user, I would like to insert a row in the customer information table using the new user ID and email address.

I tried putting some code in CreateUserWizard1_CreatedUser to get the user id. But, from what I learned, the user is created at this stage; but the user is not logged in at this point. I get an error message ...

"NullReferenceException" is handled by user code. The object is not set to an instance of the object. "

If I set a breakpoint during debugging, I see that customerId is null.

From what I read on MSDN, the CreatedUser event ... "Occurs after the membership provider has created a new website user account."

I want to add a row to the customer details table before changing the User Creation Wizard to display the Continue button. At this point, the user can go to other pages or close the application.

Is there any other event after CreateUser 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

Is there any way to register a user in the CreatedUser event to get the user ID?


Here is my code on the registry page that does not work. The error occurs in 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 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); 

...

 } 
+4
source share
1 answer

When the user goes through the registration process, HttpContext.Current.User.Identity will not be installed (anonymous access) if the user registration is not logged in.

To solve this problem, you will need to get the username from the wizard instead of context.

To fix the problem, change the following line:

 string customerId = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString(); 

:

 string customerId = Membership.GetUser((sender as CreateUserWizard).UserName).ProviderUserKey.ToString(); 
+5
source

All Articles