How to add your own user record when there is a successful ASP.Net registration?

First you merge your ASP.Net Membership table to your own database and hook up the UserId (see my previous post).


Once hooked up (see the image above) it’s time to write the code.  But where to write?  Go to Solution->Account->Register.aspx->Register.aspx.cs (for webforms but similar file name you can find on Razor and other .Net templates).  Open the file and insert a code in the method RegisterUser_CreatedUser.


Below is the sample code (a simple LINQ add new record code based on LINQ-to-SQL data source)

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
            // Cookie
            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false);

            // Save to MstUser
            Data.MstUser NewUser = new Data.MstUser();

            var Users = from u in db.Users where u.UserName==RegisterUser.UserName select u;

            if (Users.Any()) {
                NewUser.MembershipUserId = Users.First().UserId;
                var UserAccountNumber = Convert.ToDouble(db.MstUsers.Max(n => n.UserAccountNumber)) + 10000000001;
                NewUser.UserAccountNumber = UserAccountNumber.ToString().Trim().Substring(1);
                NewUser.FullName = "na";
                NewUser.Address = "na";
                NewUser.ContactNumber = "na";
                NewUser.EmailAddress = "na";
                NewUser.IsTemplate = false;
                NewUser.Particulars = "na";
                NewUser.IsAutoInventory = false;
                NewUser.IsLocked = true;



            // Redirect
            //string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            //if (!OpenAuth.IsLocalUrl(continueUrl))
            //    continueUrl = "~/";


Here is the sample running application where I used my local user to add a Setting/Manage Account page for the site.


If you find this blog helpful, you can make the donation by clicking the Paypal button below.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s