1

When Posting the following code works fine until it reaches the _userManager.CreateAsync method. No data is saved to the database.

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    private readonly ApplicationDbContext _appDbContext;

    private readonly UserManager<IdentityUser> _userManager;

    public ValuesController(UserManager<IdentityUser> userManager, ApplicationDbContext appDbContext)
    {
        _userManager = userManager;
        _appDbContext = appDbContext;
    }

    [HttpPost]
    public async Task<IActionResult> Post()
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        AppUser user = new AppUser();
        user.Email = "[email protected]";
        user.FirstName = "mark";
        user.LastName = "Macneill";
        user.UserName = "Saints22";
        await _userManager.CreateAsync(user, "P4$$word");

        return new OkObjectResult("Account created");

    }

    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2" };
    }
}
3
  • Having never used UserManager myself I did some minimal reading and it looks as though UserManager is only responsible for adding info to the UserManager object but doesn't actually save the data to the database. I read something about passing through an IUserStore to the UserManager constructor which is what is actually responsible for saving the data to a data store. Commented Oct 14, 2018 at 20:44
  • if i am not mistaken your UserManager<IdentityUser> must be UserManager<AppUser> Commented Oct 14, 2018 at 21:20
  • here is a code where i manually used the user manager for some refrence. but your code seems fine. github.com/neville-nazerane/dotnet-csp/blob/master/… Commented Oct 14, 2018 at 21:21

3 Answers 3

2

You don't seem to be using the IdentityUser directly but instead a AppUser class. This might be a class extending IdentityUser<> or IdentityUser. I am not sure if you have the rest of your setup right, so here is the process:

If you have created a custom AppUser class, let's say you created it as follows:

class AppUser : IdentityUser<int>

This would mean you have assigned the primary key as in int. However, if you extended IdentityUser instead, note that IdentityUser extends IdentityUser<string>, meaning your key is a string. Going ahead I am going to assume your key is int. If not, you can make the changes accordingly (change all ints to your key type.

In your startup class, you need to be adding the following to register this as your user class used for Identity

services.AddIdentity<AppUser, IdentityRole<int>>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

Your ApplicationDbContext needs to be defined as follows:

public class ApplicationDbContext : IdentityDbContext<AppUser, IdentityRole<int>, int>

If you don't want to use IdentityRole<int> in the above two pieces of code, you can define a custom role class as follows:

public AppUserRole : IdentityRole<int>

Once you have these setup, you need to inject UserManager<AppUser> not UserManager<IdentityUser>.

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

Comments

0

Thank you neville-nazerane

I had IdentityUser in dependency injection and all I had to do was change IdentityUser to AppUser.

Startup.cs

services.AddIdentity<AppUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

1 Comment

I would recommend adding your original startup code to your question
0

I came across a similar issue and found out that the issue was because of the password restrictions:

var result = await userManager.CreateAsync(user, password);

result.Succeeded turned out to be false.

To manage password restrictions you can update the policies as follows in Program.cs:

builder.Services.AddIdentity<AppUser, AppRole>(options => {
    // ---- customize your settings here...
    options.Password.RequireDigit = false;
    options.Password.RequiredLength = 6;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireUppercase = false;
    options.Password.RequireLowercase = false;
    options.SignIn.RequireConfirmedEmail = true;
    options.SignIn.RequireConfirmedAccount = true;
    // many different options to setup here!!!
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.