locked
Identity help - adding more fields? RRS feed

  • Question

  • User761356933 posted

    Hi, so I am using asp.net identity in web forms. It has created all the necessary tables on my remote sql server.

    I want to be able to create a supplier: supplierid, suppliername

    when creating a user on register.aspx I want to be able to assign this user to a supplier. 

    Am I best creating two new tables for suppliers and a userinfo table with columns userid,supplierid? or can I simply add to the already created user table that identity made?

    And its just a case of using SQL queries to retrieve the info or can identity get the supplierid that each user belongs to?

    Any pointers much appreciated. Sorry for all the questions, just need to make the correct start. 

    Tuesday, July 3, 2018 9:09 AM

All replies

  • User-330142929 posted

    Hi BluSky28,

    From your description, you want to add a custom column to the registered user. I recommend that you directly modify the class that inherits from IdentityUser, which indicates your user information field.

    Just like the following code snippets.

    // You can add profile data for the user by adding more properties to your ApplicationUser class.
        public class ApplicationUser : IdentityUser
        {
            public string Name { get; set; }
            public string Phone { get; set; }
    }

    ViewModel.

    public class RegisterViewModel
        {
            public string Email { get; set; }
            public string Password { get; set; }
            public string ConfirmPassword { get; set; }
            public string Name { get; set; }
            public string Phone { get; set; }
     
        }

    Razor design.

    @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        <h4>Create a new account.</h4>
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Phone, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.Phone, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="btn btn-default" value="Register" />
            </div>
        </div>

    Controller.

    [HttpPost]
            [AllowAnonymous]
            [ValidateAntiForgeryToken]
            public async Task<ActionResult> Register(RegisterViewModel model)
            {
                if (ModelState.IsValid)
                {
                    var user = new ApplicationUser { UserName = model.Email, Email = model.Email,Name=model.Name,Phone=model.Phone };
                    var result = await UserManager.CreateAsync(user, model.Password);
                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                        
                        return RedirectToAction("Index", "Home");
                    }
                    AddErrors(result);
                }
                return View(model);
            }

    Result.

    Here is official tutorial, wish it is useful to you.

    https://blogs.msdn.microsoft.com/webdev/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates/

    Feel free to let me know if you have any question.

    Best Regards

    Abraham

    Wednesday, July 4, 2018 11:48 AM
  • User-1338840997 posted

    Hi Abraham:

    Sorry to open this old thread. I had a question about adding custom fields for ASP.NET web forms applications.

    On the default template from the setup wizard, neither the file AccountViewModels.cs nor the class RegisterViewModel exists in the Models folder. Unless I'm wrong, I believe your answer applies more for MVC applications. Do you know how to implement your answer using web forms?

    Thanks,

    Friday, April 26, 2019 7:18 AM