我在 mac 机器上使用 asp.net 核心,我试图为我的 asp.net mvc web 应用程序创建一个自定义的 ApplicationUser,它与基本 IdentityUser 一起工作得非常好。
尽管遵循 Microsoft 的本指南:
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/add-user-data?view=aspnetcore-2.1&tabs=visual-studio
我面临这个错误:
{"error":"没有注册'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]'类型的服务。"}
以下是我的代码片段:
启动文件
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));
});
}
应用程序用户.cs
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser
{
[Required]
public string DrivingLicense { get; set; }
}
注册.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;
}
蛊毒传说
繁花如伊
守候你守候我
相关分类