I am using asp.net core on a mac machine, I am trying to create a custom ApplicationUser for my asp.net mvc web application which works very fine with the base IdentityUser.
Despite following this guide by Microsoft:
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/add-user-data?view=aspnetcore-2.1&tabs=visual-studio
I am faced by this error:
{"error":"No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered."}
Here are snippets of my code:
startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// [...]
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(identityDbContextConnection));
// Relevant part: influences the error
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
}
ApplicationUser.cs
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
[Required]
public string DrivingLicense { get; set; }
}
Register.cshtml.cs
public class RegisterModel : PageModel
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
private readonly ILogger<RegisterModel> _logger;
private readonly IServiceProvider _services;
public RegisterModel(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
ILogger<RegisterModel> logger,
IServiceProvider services
)
{
_userManager = userManager;
_signInManager = signInManager;
_logger = logger;
_services = services;
}
[BindProperty]
public InputModel Input { get; set; }
public string ReturnUrl { get; set; }
public class InputModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
// Added for ApplicationUser
[Required]
[Display(Name = "Driving License")]
public string DrivingLicense { get; set; }
// -----------------------------
// [...]
}
public void OnGet(string returnUrl = null)
{
ReturnUrl = returnUrl;
}
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
var user = new ApplicationUser {
UserName = Input.Email,
Email = Input.Email,
DrivingLicense = Input.DrivingLicense // property added by ApplicationUser
};
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
// If we got this far, something failed, redisplay form
return Page();
}
}
Snippets from Manage/Index.cshtml.cs
public class InputModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
// Added for ApplicationUser
[Required]
[Display(Name = "Driving License")]
public string DrivingLicense { get; set; }
// -----------------------------
[Phone]
[Display(Name = "Phone number")]
public string PhoneNumber { get; set; }
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
// [...]
// Added for ApplicationUser
if (Input.DrivingLicense != user.DrivingLicense)
{
user.DrivingLicense = Input.DrivingLicense;
}
await _userManager.UpdateAsync(user);
// -------------------------
await _signInManager.RefreshSignInAsync(user);
StatusMessage = "Your profile has been updated";
return RedirectToPage();
}
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
The only part I couldn't follow from the official microsoft guide is the one editing Account/Manage/Index.cshtml because the file was not scaffolded when I did the CLI step !
It is noted that when I replace ApplicationUser by IdentityUser in startup.cs as follow:
services.AddIdentity<IdentityUser, IdentityRole>()
the application opens but ofcourse registration does not work properly as expected.
The problem is in the '_LoginPartial.cshtml'
remove this
@using Microsoft.AspNetCore.Identity
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
Add this
@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
In dotnet core 2.1 I faced the same issue, below steps solved my problems
1 ) Extend the IdentityUser or IdentityRole
public class ApplicationUser : IdentityUser<Guid>
{
public DateTime JoinTime { get; set; } = DateTime.Now;
public DateTime DOB { get; set; } = Convert.ToDateTime("01-Jan-1900");
}
public class ApplicationRole : IdentityRole<Guid>
{
public string Description { get; set; }
}
2 ) Update ApplicationDbContext class
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
}
3 ) Update Stratup.cs ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders();
}
Update _LoginPartial.cshtml (shared --> view )
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
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