1

I want to insert a value in DB and I am using the Asp.Net Identity framework, in my Register method when a new user registers I also want to add a new Id which is client_id by default this id is not inserted by the identity framework?

if (User.IsInRole("Avanceon")) 
{
    var MaxCustomerId = db.AspNetUsers.Max(x => x.Customer_Id);
    if (ModelState.IsValid)
    {   
        var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            // Assign Role to user Here 
            await this.UserManager.AddToRoleAsync(user.Id, model.RoleName);

            // Ends Here
            // await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
            ViewBag.userCreated = user.UserName + " Created Successfully";
            ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
            MaxCustomerId = MaxCustomerId + 1;
            // db.AspNetUsers.LastOrDefault().Customer_Id = MaxCustomerId;
            db.SaveChanges();
            return View();
            //return RedirectToAction("Index", "Home");
        }

        AddErrors(result);
    }
}

ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
return View(model);
1
  • There was a good series on Channel9 around customising ASP.NET Identity authentication. I think this episode is what you're after. IT is very similar to the below answer but includes the changes required on the Entity Framework end. channel9.msdn.com/Series/… Commented Jan 6, 2020 at 9:34

1 Answer 1

2

You normally can't simply insert any data to AspNetUsers table. You'll need create a class and inherit it from IdentityUser and do the necessary changes in your class.

With code first you should create a class say ApplicationUser which inherits IdentityUser. And add the property there:

public class ApplicationUser : IdentityUser
{
    [Required]
    public string ClientID { get; set; }
}

And then target ApplicationUser (instead of IdentityUser) in your code:

var user = new ApplicationUser
{
    UserName = model.UserName,
    Email = model.Email,
    ClientID = model.ClientID,
};

Make below changes as well:

In ConfigureServices method of Startup.cs

services.AddIdentity<ApplicationUser, IdentityRole>();

In AccountController,

private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;

Add migration for this change to take effect

Sign up to request clarification or add additional context in comments.

3 Comments

Hi i tried your solution but still not inserting the value???\
You will need to create migration for this as well. Then it should work
If the user already exists and you just want to update the Client ID, what do you use instead of var user = new ApplicationUser { }; ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.