Reputation: 25
I have the following code in the AccountsController :
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Products.Helpers;
using Products.Models;
using Products.ViewModels;
namespace Products.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AccountsController : Controller
{
private readonly ContextUsers _appDbContext;
private readonly UserManager<AppUser> _userManager;
private readonly IMapper _mapper;
public AccountsController(ContextUsers context,UserManager<AppUser> userManager,IMapper mapper)
{
_appDbContext = context;
_userManager = userManager;
_mapper = mapper;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody]RegistrationViewModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var userIdentity = _mapper.Map<AppUser>(model);
var result = await _userManager.CreateAsync(userIdentity, model.Password);
if (!result.Succeeded) return new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState));
await _appDbContext.Customers.AddAsync(new Customer { IdentityId = userIdentity.Id, Location = model.Location });
await _appDbContext.SaveChangesAsync();
return new OkObjectResult("Account created");
}
}
}
And I have the following code in the AppUser class:
public class AppUser : IdentityUser
{
// Extended Properties
public string FirstName { get; set; }
public string LastName { get; set; }
public long? FacebookId { get; set; }
public string PictureUrl { get; set; }
}
And the following Customer class:
public class Customer
{
public int Id { get; set; }
public string IdentityId { get; set; }
public AppUser Identity { get; set; } // navigation property
public string Location { get; set; }
public string Locale { get; set; }
public string Gender { get; set; }
}
When i make a post request in postman i get the following error:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Products.Models.AppUser]' while attempting to activate 'Products.Controllers.AccountsController'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
I am trying to create a user but when I make the post request doesn't even enter the controller so I can't debug
ConfigureServices method from startup.cs:
public void ConfigureServices(IServiceCollection services)
{
.AddCors(o => o.AddPolicy("AllowAllOrigins", builder =>
{
builder.AllowAnyMethod()
.AllowAnyHeader()
.AllowAnyOrigin();
}));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<Context>(options =>
options.UseSqlServer(Configuration.GetConnectionString("LaprDB")));
services.AddDbContext<ContextUsers>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyDbConnection")));
services.AddHostedService<TimedHostedService>();
services.AddAutoMapper();
}
Upvotes: 1
Views: 2460
Reputation: 119017
You are missing a line to add the Identity object to the DI container. You need to call the AddDefaultIdentity
method. Add this line to the ConfigureServices
method in your Startup
class:
services.AddDefaultIdentity<AppUser>()
.AddEntityFrameworkStores<ContextUsers>();
See here for more information on setting up Identity.
Upvotes: 2