I am using a code from "https://fullstackmark.com/post/13/jwt-authentication-with-aspnet-core-2-web-api-angular-5-net-core-identity-and-facebook-login" but I can't get past the AccountsController since I get this error: "The name 'Errors' does not exist in the current context"
This is my code:
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using webapi.DataContext;
using webapi.Models;
using webapi.ViewModels;
namespace webapi.Controllers
{
[Route("api/controller")]
[ApiController]
public class AccountsController : ControllerBase
{
private readonly UserManager<AppUser> _userManager;
private readonly IMapper _maper;
private readonly ApplicationDbContext _appDbContext;
public AccountsController(UserManager<AppUser> userManager, IMapper maper, ApplicationDbContext appDbContext)
{
_appDbContext = appDbContext;
_maper = maper;
_userManager = userManager;
}
[HttpPost]
public async Task<IActionResult> Post(RegistrationViewModel model)
{
if(!ModelState.IsValid){
return BadRequest(ModelState);
}
var userIdentity = _maper.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");
}
}
}
I get the error on this line:
if (!result.Succeeded) return new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState));

Can you please show me how to this right? Than you.
The Errors is no more than a plain custom helper class.
To fix your issue, simply add a class as below:
public static class Errors
{
public static ModelStateDictionary AddErrorsToModelState(IdentityResult identityResult, ModelStateDictionary modelState)
{
foreach (var e in identityResult.Errors)
{
modelState.TryAddModelError(e.Code, e.Description);
}
return modelState;
}
public static ModelStateDictionary AddErrorToModelState(string code, string description, ModelStateDictionary modelState)
{
modelState.TryAddModelError(code, description);
return modelState;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With