Нет службы для типа Identity.RoleManager - Identity.IdentityRole была зарегистрирована ошибка - PullRequest
0 голосов
/ 24 июня 2019

Я использую Microsoft Identity впервые. Я настроил пользователей и роли с IdentityUser и IdentityRole. Я хочу назначить роль пользователям в Startup.cs. Я написал метод, чтобы сделать это,

private async Task CreateUserRoles(IServiceProvider serviceProvider) {


      var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole<int>>>();
      var userManager = serviceProvider.GetRequiredService<UserManager<User>>();

      var roleName = "Super Admin";
      var roleCheck = await roleManager.RoleExistsAsync(roleName);
      if (!roleCheck) {
            Role role = new Role();
            role.Name = roleName;
            IdentityResult result = roleManager.CreateAsync(role).Result;
            //IdentityResult roleResult = await roleManager.CreateAsync(new IdentityRole<int>(roleName));
      }
      User user = new User();
      user.UserName = "someone";
      user.Password = "someone";
      user.Email = "someone@gmail.com";
      ApplicationDbContext context = new ApplicationDbContext();
      context.Users.Add(user);
      context.SaveChanges();
      user = await userManager.FindByEmailAsync("someone@gmail.com");

      await userManager.AddToRoleAsync(user, roleName);
}

Однако есть проблема:

No service for type 
'Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole1[System.Int32]]' has been registered.

Как я могу это исправить?

Вот это Startup.cs

  public class Startup {
        public Startup(IConfiguration configuration) {
              Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services) {
              services.Configure<CookiePolicyOptions>(options => {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    options.CheckConsentNeeded = context => true;
                    options.MinimumSameSitePolicy = SameSiteMode.None;
              });

              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
              services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();


              // Add MVC services to the services container.
              services.AddMvc();
              services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache
              services.AddSession(opt => { opt.Cookie.IsEssential = true; });

              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                          .AddRazorPagesOptions(options => {
                                options.AllowAreas = true;
                                options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Settings");
                                options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
                          });
              services.AddDbContext<ApplicationDbContext>(options =>
                                options.UseNpgsql("User ID = postgres; Password = sa; Host = localhost; Port = 5432; Database = CCN; Pooling = true;"));

              services.ConfigureApplicationCookie(options => {
                    options.LoginPath = $"/Account/Login"; //options.LoginPath = $"/Identity/Account/Login";
                    options.LogoutPath = $"/Account/Logout";
                    options.AccessDeniedPath = $"/Account/AccessDenied";
              });



              //Password settings
              services.AddIdentity<User, Role>(o => {
                    o.Password.RequireDigit = false;
                    o.Password.RequireLowercase = false;
                    o.Password.RequireUppercase = false;
                    o.Password.RequireNonAlphanumeric = false;
                    //o.Password.RequiredLength = 3;

              }).AddEntityFrameworkStores<ApplicationDbContext>().AddRoles<IdentityRole>()
              .AddDefaultTokenProviders();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider services) {

              app.UseDeveloperExceptionPage();
              app.UseStatusCodePages();
              if (env.IsDevelopment()) {
                    app.UseDeveloperExceptionPage();
              }
              else {
                    app.UseExceptionHandler("/Home/Index");
                    app.UseHsts();
              }

              app.UseHttpsRedirection();
              app.UseStaticFiles();
              app.UseCookiePolicy();
              //Enable session 
              app.UseSession();


              app.UseAuthentication();

              app.UseMvc(routes => {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}");
              });

              //Create user role and assign it  
              CreateUserRoles(services).Wait();

        }

  }

Ответы [ 2 ]

0 голосов
/ 26 июня 2019

Я пробовал эти решения, но они не работали. Затем я узнаю, что создал сущность с именем Role, которая является производной от IdentityRole, а ее тип данных id - int. Я изменил

var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole<int>>>();

эта строка с

var roleManager = serviceProvider.GetRequiredService<RoleManager<Role>>();

этот. Тогда это работает. С другой стороны, спасибо за вашу помощь!

0 голосов
/ 25 июня 2019

Служба для типа 'Microsoft.AspNetCore.Identity.RoleManager1 [Microsoft.AspNetCore.Identity.IdentityRole1 [System.Int32]]' не зарегистрирована.

При регистрации IdentityRole для удостоверения AspNetCore тип RoleManager <> будет зарегистрирован в ServiceCollection как RoleManager<IdentityRole>.

Когда вы захотите разрешить RoleManager<>, укажите модель роли идентификации, зарегистрированную при запуске.в качестве параметра типа. Что будет RoleManager<IdentityRole> в вашем конкретном случае.

При вызове GetRequiredService<RoleManager<IdentityRole>>() для получающегося поставщика услуг, GetRequiredService<RoleManager<IdentityRole>>() сгенерирует указанное выше исключение.

Внесите следующие изменения:

В CreateUserRoles

var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();

Зарегистрируйте службы ролей в контейнере DI (выберите один из двух методов)

1.Используйте AddIdentity ()

services.AddIdentity<User, IdentityRole()
        .AddDefaultUI()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

2.Используйте AddDefaultIdentity, включите роли, используя метод [AddRoles][1]

services.AddDefaultIdentity<User>()
        .AddRoles<IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

Ссылка: AddDefaultIdentity и AddIdentity

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...