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).

b1

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.

b2

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;

                db.MstUsers.InsertOnSubmit(NewUser);
                db.SubmitChanges();
            }

            Response.Redirect("~/");

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

        }

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

b3

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


					
Advertisements
About

Software developer living in the Philippines.

Posted in ASP.NET

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Author

Harold Glenn P. Minerva
Software Developer / Tech Enthusiast
Living in the Philippines

View Harold Glenn Minerva's profile on LinkedIn

Instagram

Software Engineer - Seasonal and Range Trading Software. Magenta Trader is a powerful stock market visualization software that increases your probability of trading success.

Software Architect and Founder - Easyfis.com is a multi-tenant cloud-based Software-as-a-Service (SaaS) business app that caters to micro, small and medium trading businesses.

CTO and Co-Founder - We give your company the leverage by providing innovative software solutions products such as Point-of-Sales (POS), Financial Information System (FMIS), Payroll and DTR (HRIS), and many more.

%d bloggers like this: