locked
Adding Company Name field to ASP.Net identity login RRS feed

  • Question

  • User64294366 posted

    Hello - I am wanting to increase the security on an web app that was written at least 10 years ago. I created a web forms project in Visual Studio 2017 using the default web forms template with Identity and migrated the mdf file to a remote database. I created a new AspNetUser table and added a CompanyName column to the table. The registration and login is working however I have not been able to find out how to add the new field as part of the registration and login.

    In my search I see additional fields added however the examples I've seen are in MVC of which I am not well versed. Can someone point me in the correct direction?

    Thank-you.

    Friday, January 26, 2018 4:21 PM

All replies

  • User533502567 posted

    The registration and login is working however I have not been able to find out how to add the new field as part of the registration and login.

    Check Login.aspx page, and update the markup near the end of the loginForm section. This should give you an idea about updating default login pages: Secure ASP.NET Web Forms

    Friday, January 26, 2018 4:40 PM
  • User64294366 posted

    Thank-you, I'll take a look

    Friday, January 26, 2018 4:50 PM
  • User283571144 posted

    Hi RDowdall,

    Hello - I am wanting to increase the security on an web app that was written at least 10 years ago. I created a web forms project in Visual Studio 2017 using the default web forms template with Identity and migrated the mdf file to a remote database. I created a new AspNetUser table and added a CompanyName column to the table. The registration and login is working however I have not been able to find out how to add the new field as part of the registration and login.

    In my search I see additional fields added however the examples I've seen are in MVC of which I am not well versed. Can someone point me in the correct direction?

    As far as I know, the identity use the EF code-first to achieve model bind in the webform.

    If you want to add new column to the aspnetuser table, I suggest you could also use EF code-first.

    I suggest you could follow below steps to achieve add the company to the register page.

    1.Go to Models folder → Open IdentityModels.cs → ApplicationUser class and add the property:

            public virtual string Company { get; set; }
    

    2.Build the project

    3.Go to TOOLS menu → Nuget Package Manager → click Package Manager Console

    4.Type Enable-Migrations and press Enter and wait until the task get completed. You will see a response which says:

       Checking if the context targets an existing database...
       Code First Migrations enabled for project WebApplication1.

    5.Type Add-Migration "Company" and press Enter and wait until the task get completed. You will see a response which says:

    Scaffolding migration 'xxxxx'.
    The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration xxxxx' again.

    6.Type Update-Database and press Enter and wait until the task get completed. You will see a response which says:

    Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
    Applying explicit migrations: [201801300632328_testa].
    Applying explicit migration: 201801300632328_testa.
    Running Seed method.

    7.At this step if you refresh SQL Server Object Explorer and go to database and see tables, under dbo.AspNetUsers under columns, you will see the Companyname field.

    8.Go to Account folder → Open Register.aspx add below textbox under the confirm password.

               <div class="form-group">
                       <asp:Label runat="server" AssociatedControlID="CompanyName" CssClass="col-md-2 control-label">CompanyName</asp:Label>
                            <div class="col-md-10">
                                <asp:TextBox runat="server" ID="CompanyName"   CssClass="form-control" />
                                <asp:RequiredFieldValidator runat="server" ControlToValidate="CompanyName" CssClass="text-danger" ErrorMessage="The CompanyName field is required." />
                            </div>
                        </div>

    9.Go code-behind change the CreateUser_Click method's as below 

            protected void CreateUser_Click(object sender, EventArgs e)
            {
                var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
                var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text,Company= CompanyName.Text };
                IdentityResult result = manager.Create(user, Password.Text);
                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    //string code = manager.GenerateEmailConfirmationToken(user.Id);
                    //string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
                    //manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");
    
                    signInManager.SignIn( user, isPersistent: false, rememberBrowser: false);
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else 
                {
                    ErrorMessage.Text = result.Errors.FirstOrDefault();
                }
            }

    Then it will work well.

    The result looks like this:

    Best Regards,

    Brando

    Tuesday, January 30, 2018 6:45 AM
  • User64294366 posted

    Sorry for the delay in getting back to you, but it's been hell week as an intranet application that has been in production for six months suddenly quit working. I am going to review your reply today and give it a try.

    Thursday, February 1, 2018 2:14 PM