locked
ASP.NET Identity - Add New field to Registration page! Webforms not MVC RRS feed

  • Question

  • User-1664485818 posted

    Hi folks, I would like to add a new Birthday field to the Registration page, looking for a tutorial on how to achieve this.

    I can't seem to find any for Web forms. I can find loads for MVC, spec below

    • I'm using 4.5 net Microsoft Studio 2013
    • Project ASP.NET Web Application 
    • Template - Web Forms

    Wednesday, February 17, 2016 4:10 PM

Answers

  • User614698185 posted

    Hi brucey,

    If you want to adding fields to User's Profile in Web forms application, the steps are similar with in MVC, you could refer to the following steps:

    Step 1: Execute the command “Enable-Migrations”, from ‘Package Manager Console’, the NuGet power tool.

    Step 2: Add new properties in Models/IdentityModels.cs. You could refer to the following code.

    public class MyUserInfo : IdentityUser
    {
            public string Birthday { get; set; }
    }
    
    public class ApplicationDbContext : IdentityDbContext<MyUserInfo>
    {
         public ApplicationDbContext() : base("DefaultConnection")
         {
         }
    }
    
    public class UserManager : UserManager<MyUserInfo>
    {
        public UserManager() : base(new UserStore<MyUserInfo>(new ApplicationDbContext()))
        {
        }
    }

    Step 3: Add New Migration.

    Once we added the properties, bring the Package Manager Console and execute following command.

    Add-Migration "Birthday"

    This command will generate a big blog database script file, now execute following command to run this script file against database.

    Update-Database

    Step 4:  When we create User. Please refer to the following code.

    protected void CreateUser_Click(object sender, EventArgs e)
    {
        // Default UserStore constructor uses the default connection string named: DefaultConnection
        var userStore = new UserStore<MyUserInfo>();
        var manager = new UserManager<MyUserInfo>(userStore);
    
        var user = new MyUserInfo() { UserName = UserName.Text, Birthday = txtBirthday.Text };
        IdentityResult result = manager.Create(user);
    
        if (result.Succeeded)
        {
            StatusMessage.Text = string.Format("User {0} was created successfully!", user.UserName);
        }
        else
        {
            StatusMessage.Text = result.Errors.FirstOrDefault();
        }
    }

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 18, 2016 5:53 AM

All replies

  • User614698185 posted

    Hi brucey,

    If you want to adding fields to User's Profile in Web forms application, the steps are similar with in MVC, you could refer to the following steps:

    Step 1: Execute the command “Enable-Migrations”, from ‘Package Manager Console’, the NuGet power tool.

    Step 2: Add new properties in Models/IdentityModels.cs. You could refer to the following code.

    public class MyUserInfo : IdentityUser
    {
            public string Birthday { get; set; }
    }
    
    public class ApplicationDbContext : IdentityDbContext<MyUserInfo>
    {
         public ApplicationDbContext() : base("DefaultConnection")
         {
         }
    }
    
    public class UserManager : UserManager<MyUserInfo>
    {
        public UserManager() : base(new UserStore<MyUserInfo>(new ApplicationDbContext()))
        {
        }
    }

    Step 3: Add New Migration.

    Once we added the properties, bring the Package Manager Console and execute following command.

    Add-Migration "Birthday"

    This command will generate a big blog database script file, now execute following command to run this script file against database.

    Update-Database

    Step 4:  When we create User. Please refer to the following code.

    protected void CreateUser_Click(object sender, EventArgs e)
    {
        // Default UserStore constructor uses the default connection string named: DefaultConnection
        var userStore = new UserStore<MyUserInfo>();
        var manager = new UserManager<MyUserInfo>(userStore);
    
        var user = new MyUserInfo() { UserName = UserName.Text, Birthday = txtBirthday.Text };
        IdentityResult result = manager.Create(user);
    
        if (result.Succeeded)
        {
            StatusMessage.Text = string.Format("User {0} was created successfully!", user.UserName);
        }
        else
        {
            StatusMessage.Text = result.Errors.FirstOrDefault();
        }
    }

    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 18, 2016 5:53 AM
  • User-1664485818 posted

    Thanks work a treat :)

    Thursday, February 18, 2016 3:06 PM