i have this error when i tried to create user and role in the startup file, can you please help me solve this issue. The error is coming from the serviceprovider in this line -----> var roleManager = serviceProvider.GetRequiredService<RoleManager>();
**
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.AddControllersWithViews(); services.AddDbContext<LoginDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("LoginConnection"))); //ajouter le service d'authentification Identity services.AddIdentity<LoginUser, LoginRole>(options => { options.Password.RequiredLength = 6; options.Password.RequiredUniqueChars = 2; options.Password.RequireLowercase = false; options.Password.RequireUppercase = false; options.Password.RequireNonAlphanumeric = false; options.Password.RequireDigit = false; options.SignIn.RequireConfirmedEmail = false; }).AddEntityFrameworkStores<LoginDbContext>() .AddDefaultTokenProviders(); services.ConfigureApplicationCookie(options => { options.LoginPath = "/Login/Login"; options.ReturnUrlParameter = "ReturnUrl"; options.LogoutPath = "/Login/Logout"; options.AccessDeniedPath = "/Login/AccessDenied"; options.ExpireTimeSpan = new TimeSpan(0, 15, 0); }); //ajouter le service MVC services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseAuthentication(); // ajouter le module d'authentification au middleware mvc app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); CreateRoles(serviceProvider); } private void CreateRoles(IServiceProvider serviceProvider) { var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>(); var userManager = serviceProvider.GetRequiredService<UserManager<LoginUser>>(); Task<IdentityResult> roleResult; string email = "someone@somewhere.com"; //Check that there is an Administrator role and create if not Task<bool> hasAdminRole = roleManager.RoleExistsAsync("Administrator"); hasAdminRole.Wait(); if (!hasAdminRole.Result) { roleResult = roleManager.CreateAsync(new IdentityRole("Administrator")); roleResult.Wait(); } //Check if the admin user exists and create it if not //Add to the Administrator role Task<LoginUser> testUser = userManager.FindByEmailAsync(email); testUser.Wait(); if (testUser.Result == null) { LoginUser administrator = new LoginUser(); administrator.Email = email; administrator.UserName = email; Task<IdentityResult> newUser = userManager.CreateAsync(administrator, "_AStrongP@ssword!"); newUser.Wait(); if (newUser.Result.Succeeded) { Task<IdentityResult> newUserRole = userManager.AddToRoleAsync(administrator, "Administrator"); newUserRole.Wait(); } } } }** https://stackoverflow.com/questions/66468015/error-no-service-for-type-microsoft-aspnetcore-identity-rolemanager1microsoft March 04, 2021 at 11:09AM
没有评论:
发表评论