猿问

没有类型“Microsoft.AspNetCore.Identity.UserManager”的服务

我在 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;

    }


qq_遁去的一_1
浏览 193回答 3
3回答

蛊毒传说

问题出在“_LoginPartial.cshtml”中删除这个@using Microsoft.AspNetCore.Identity@inject SignInManager<IdentityUser> SignInManager@inject UserManager<IdentityUser> UserManager添加这个@using Microsoft.AspNetCore.Identity@inject SignInManager<ApplicationUser> SignInManager@inject UserManager<ApplicationUser> UserManager

繁花如伊

在 dotnet core 2.1 中,我遇到了同样的问题,以下步骤解决了我的问题1 ) 扩展 IdentityUser 或 IdentityRolepublic class ApplicationUser : IdentityUser<Guid>{&nbsp; &nbsp; public DateTime JoinTime { get; set; } = DateTime.Now;&nbsp; &nbsp; public DateTime DOB { get; set; } = Convert.ToDateTime("01-Jan-1900");}public class ApplicationRole : IdentityRole<Guid>{&nbsp; &nbsp; public string Description { get; set; }}2)更新ApplicationDbContext类public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>{&nbsp; &nbsp; public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)&nbsp; &nbsp; {&nbsp; &nbsp; }}3) 更新 Stratup.cs 配置服务public void ConfigureServices(IServiceCollection services){&nbsp; &nbsp; services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);&nbsp; &nbsp; services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();&nbsp; &nbsp; services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));&nbsp; &nbsp; services.AddIdentity<ApplicationUser, ApplicationRole>().AddEntityFrameworkStores<ApplicationDbContext>()&nbsp; &nbsp; &nbsp; &nbsp; .AddDefaultUI()&nbsp; &nbsp; &nbsp; &nbsp; .AddDefaultTokenProviders();}更新 _LoginPartial.cshtml(共享 --> 查看)@inject SignInManager<ApplicationUser> SignInManager@inject UserManager<ApplicationUser> UserManager

守候你守候我

核心2有同样的问题。你需要检查一个更大的面积是在文件_ManageNav.cshtml有你有更新的订单@inject SignInManager<IdentityUser> SignInManager有@inject SignInManager<YOURCUSTOMMODEL> SignInManager。希望有帮助
随时随地看视频慕课网APP
我要回答