osyris zosar

osyris zosar

  • NA
  • 289
  • 24k

MVC Adding Roles Error

Mar 16 2021 6:46 AM
Im trying to add roles but i keep getting the same error:
 
my startup.cs:
 
  1. using Mastervisualidentity.Models;  
  2. using Microsoft.AspNetCore.Builder;  
  3. using Microsoft.AspNetCore.Hosting;  
  4. using Microsoft.AspNetCore.HttpsPolicy;  
  5. using Microsoft.Extensions.Configuration;  
  6. using Microsoft.Extensions.DependencyInjection;  
  7. using Microsoft.Extensions.Hosting;  
  8. using System;  
  9. using System.Collections.Generic;  
  10. using System.Linq;  
  11. using System.Threading.Tasks;  
  12. using Microsoft.EntityFrameworkCore;  
  13. using Microsoft.EntityFrameworkCore.Design;  
  14. using Microsoft.AspNetCore.Identity;  
  15.   
  16.   
  17. namespace Mastervisualidentity  
  18. {  
  19.     public class Startup  
  20.     {  
  21.         public Startup(IConfiguration configuration)  
  22.         {  
  23.             Configuration = configuration;  
  24.         }  
  25.   
  26.         public IConfiguration Configuration { get; }  
  27.   
  28.         // This method gets called by the runtime. Use this method to add services to the container.  
  29.         public void ConfigureServices(IServiceCollection services)  
  30.         {  
  31.               
  32.   
  33.             services.AddIdentity<AppUser, AppRole>(options =>  
  34.              {  
  35.                  options.User.RequireUniqueEmail = false;  
  36.                  options.Password.RequireDigit = false;  
  37.                  options.Password.RequiredLength = 3;  
  38.                  options.Password.RequireNonAlphanumeric = false;  
  39.                  options.Password.RequiredUniqueChars = 0;  
  40.                  options.Password.RequireLowercase = false;  
  41.                  options.Password.RequireUppercase = false;  
  42.   
  43.   
  44.              }).AddEntityFrameworkStores<IdentityAppContext>()  
  45.              .AddDefaultTokenProviders().AddRoles<AppRole>();  
  46.   
  47.   
  48.             services.AddDbContext<IdentityAppContext>(options =>  
  49.             options.UseSqlServer(Configuration.GetConnectionString("Master")));  
  50.   
  51.             services.AddControllersWithViews();  
  52.             services.AddRazorPages();  
  53.   
  54.               
  55.   
  56.             services.ConfigureApplicationCookie(config =>  
  57.             {  
  58.                 config.Cookie.Name = "Login_Cookie1";  
  59.                 config.LoginPath = "/Account/Login";  
  60.             });  
  61.   
  62.             services.AddAuthorization(options =>  
  63.             {  
  64.                 options.AddPolicy("RequireAdministrator",  
  65.                     policy => policy.RequireRole("Administrator"));  
  66.   
  67.             });  
  68.   
  69.         }  
  70.   
  71.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  72.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
  73.         {  
  74.             if (env.IsDevelopment())  
  75.             {  
  76.                 app.UseDeveloperExceptionPage();  
  77.             }  
  78.             else  
  79.             {  
  80.                 app.UseExceptionHandler("/Home/Error");  
  81.                 app.UseHsts();  
  82.             }  
  83.             app.UseHttpsRedirection();  
  84.             app.UseStaticFiles();  
  85.   
  86.             app.UseRouting();  
  87.   
  88.             app.UseAuthentication();  
  89.             app.UseAuthorization();  
  90.   
  91.   
  92.             app.UseEndpoints(endpoints =>  
  93.             {  
  94.                 endpoints.MapControllerRoute(  
  95.                     name: "default",  
  96.                     pattern: "{controller=Account}/{action=Index}/{id?}");  
  97.             });  
  98.         }  
  99.     }  
  100. }  
 
 
the error I keep getting:  
  1. System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Mastervisualidentity.Controllers.AccountController'.  
  2.    at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)  
  3.    at lambda_method2(Closure , IServiceProvider , Object[] )  
  4.    at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)  
  5.    at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)  
  6.    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)  
  7.    at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()  
  8. --- End of stack trace from previous location ---  
  9.    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)  
  10.    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)  
  11.    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)  
  12.    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()  
  13. --- End of stack trace from previous location ---  
  14.    at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)  
  15.    at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)  
  16.    at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)  
  17.    at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)  
  18.    at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)  

Answers (4)