Im trying to add roles but i keep getting the same error:
my startup.cs:
- using Mastervisualidentity.Models;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.HttpsPolicy;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.EntityFrameworkCore.Design;
- using Microsoft.AspNetCore.Identity;
- namespace Mastervisualidentity
- {
- 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.AddIdentity
(options => - {
- options.User.RequireUniqueEmail = false;
- options.Password.RequireDigit = false;
- options.Password.RequiredLength = 3;
- options.Password.RequireNonAlphanumeric = false;
- options.Password.RequiredUniqueChars = 0;
- options.Password.RequireLowercase = false;
- options.Password.RequireUppercase = false;
- }).AddEntityFrameworkStores
() - .AddDefaultTokenProviders().AddRoles
(); - services.AddDbContext
(options => - options.UseSqlServer(Configuration.GetConnectionString("Master")));
- services.AddControllersWithViews();
- services.AddRazorPages();
- services.ConfigureApplicationCookie(config =>
- {
- config.Cookie.Name = "Login_Cookie1";
- config.LoginPath = "/Account/Login";
- });
- services.AddAuthorization(options =>
- {
- options.AddPolicy("RequireAdministrator",
- policy => policy.RequireRole("Administrator"));
- });
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- app.UseHsts();
- }
- app.UseHttpsRedirection();
- app.UseStaticFiles();
- app.UseRouting();
- app.UseAuthentication();
- app.UseAuthorization();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllerRoute(
- name: "default",
- pattern: "{controller=Account}/{action=Index}/{id?}");
- });
- }
- }
- }
the error I keep getting:
- System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.IdentityRole]' while attempting to activate 'Mastervisualidentity.Controllers.AccountController'.
- at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
- at lambda_method2(Closure , IServiceProvider , Object[] )
- at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.
b__0(ControllerContext controllerContext) - at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.
g__CreateController|0(ControllerContext controllerContext) - at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
- at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
- --- End of stack trace from previous location ---
- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.
g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) - at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
- --- End of stack trace from previous location ---
- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.
g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) - at Microsoft.AspNetCore.Routing.EndpointMiddleware.
g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) - at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
- at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
- at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
osyris zosarPosted Mar 17, 2021, 12:22 AM
in _loginPartial.cshtml
Asma KhalidPosted Mar 16, 2021, 8:16 AM
osyris zosarPosted Mar 16, 2021, 7:19 AM
Kiran MohantyPosted Mar 16, 2021, 6:54 AM