4

I have added 3 new fields to my Register.aspx template (FirstName, LastName, BuisnessName). I have updated the database schema but when I register a new user, the user is created but the new fields are not added to the user record. I have extended the ApplicationUser class using IdentityUser. What have I done wrong or left out?

Any help would be greatly appreciated. Here is my code:

Register.aspx.cs

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, FirstName = FirstName.Text, LastName = LastName.Text, BusinessName = BusinessName.Text };

  manager.Create(user, FirstName.Text);
  manager.Create(user, LastName.Text);
  manager.Create(user, BusinessName.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 http://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>.");
    if (user.EmailConfirmed)
    {

    signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
    }
    else
    {
      ErrorMessage.Text = "An email has been sent to your account. Please view the email and confirm your account to complete the registration process.";
    }
  }
  else
  {
    ErrorMessage.Text = result.Errors.FirstOrDefault();
  }
}

Register.aspx

    <div class="form-horizontal">
    <h4>Create a new account</h4>
    <hr />
    <asp:ValidationSummary runat="server" CssClass="text-danger" />
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="FirstName" CssClass="col-md-2 control-label">First Name</asp:Label>
        <div class="col-md-10">
            <asp:TextBox runat="server" ID="FirstName" CssClass="form-control" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="FirstName"
                CssClass="text-danger" ErrorMessage="The First Name field is required." />
        </div>
    </div>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="LastName" CssClass="col-md-2 control-label">Last Name</asp:Label>
        <div class="col-md-10">
            <asp:TextBox runat="server" ID="LastName" CssClass="form-control" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="LastName"
                CssClass="text-danger" ErrorMessage="The Last Name field is required." />
        </div>
    </div>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="BusinessName" CssClass="col-md-2 control-label">Business Name</asp:Label>
        <div class="col-md-10">
            <asp:TextBox runat="server" ID="BusinessName" CssClass="form-control" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="BusinessName"
                CssClass="text-danger" ErrorMessage="The Business Name field is required." />
        </div>
    </div>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="Email" CssClass="col-md-2 control-label">Email</asp:Label>
        <div class="col-md-10">
            <asp:TextBox runat="server" ID="Email" CssClass="form-control" TextMode="Email" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="Email"
                CssClass="text-danger" ErrorMessage="The email field is required." />
        </div>
    </div>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="Password" CssClass="col-md-2 control-label">Password</asp:Label>
        <div class="col-md-10">
            <asp:TextBox runat="server" ID="Password" TextMode="Password" CssClass="form-control" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
                CssClass="text-danger" ErrorMessage="The password field is required." />
        </div>
    </div>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="ConfirmPassword" CssClass="col-md-2 control-label">Confirm password</asp:Label>
        <div class="col-md-10">
            <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" CssClass="form-control" />
            <asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
                CssClass="text-danger" Display="Dynamic" ErrorMessage="The confirm password field is required." />
            <asp:CompareValidator runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword"
                CssClass="text-danger" Display="Dynamic" ErrorMessage="The password and confirmation password do not match." />
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <asp:Button runat="server" OnClick="CreateUser_Click" Text="Register" CssClass="btn btn-default" />
        </div>
    </div>
</div>

IdentityModels.cs

public class ApplicationUser : IdentityUser
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string BusinessName { get; set; }

  public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        return Task.FromResult(GenerateUserIdentity(manager));
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

}

joey.coyle
  • 107
  • 1
  • 9

1 Answers1

1

Couple of things I have identified from your code:

The method manager.Create() expects an object of type ApplicationUser and the optional second parameter is string password.

You should remove these three lines:

IdentityResult result2 = manager.Create(user, FirstName.Text);
IdentityResult result3 = manager.Create(user, LastName.Text);
IdentityResult result4 = manager.Create(user, BusinessName.Text);

Finally, I am not sure if the properties in your ApplicationUser should be marked as virtual. Please see this SO post for a better explanation.

My suggestion is to remove virtual from the following properties:

  public virtual string FirstName { get; set; }
  public virtual string LastName { get; set; }
  public virtual string BusinessName { get; set; }
Community
  • 1
  • 1
Seany84
  • 5,526
  • 5
  • 42
  • 67
  • I have updated my coded as you suggested (See Above) but the fields are still not added when the new user is created. Where do I create the manager.create statements for the 3 new fields. This was being handled by the lines you had me deleted – joey.coyle Dec 15 '15 at 16:17
  • Can you tell me if the values for the new properties are NULL or an empty string in the database ? – Seany84 Dec 15 '15 at 16:24
  • The 3 fields can be seen in the DB schema but the fields are not present in the database table after the new user is added. – joey.coyle Dec 15 '15 at 16:27
  • with no users in the table and the db schema showing the new fields but they do not show in the list of fields when using "Show Table Data" – joey.coyle Dec 15 '15 at 16:43
  • So, you have 0 users in your `AspNetUsers` table? What database are you using ? – Seany84 Dec 15 '15 at 16:45
  • I am using a locally create SQL DB that is created by my asp.net webform appliation. I am developing locally on my machine using IIS Express. yes, I have zero users because I keep deleting the user I am creating to test with. – joey.coyle Dec 15 '15 at 16:49
  • Do you have a `DefaultConnection` specified in your `Web.config` in the `` section ? – Seany84 Dec 15 '15 at 16:55
  • I deleted the db and created a new one and the new fields show. Thank you for your help – joey.coyle Dec 15 '15 at 17:11