I
I
Ivan_Worker2021-12-20 23:08:34
Database
Ivan_Worker, 2021-12-20 23:08:34

It is not possible to pass through the restriction on roles, although the user has everything that is needed. Maybe I don't notice something?

public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<UserId>(options =>
            {
                options.Password.RequireDigit = true;
                options.Password.RequiredLength = 5;
                options.Password.RequireUppercase = true;
                options.Lockout.MaxFailedAccessAttempts = 5;
                options.User.RequireUniqueEmail = true;
                options.SignIn.RequireConfirmedEmail = false;
            })
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>();
            
            services.AddAuthorization(options => {
                options.AddPolicy("AdministratorRole", policy =>
                    policy.RequireRole("Administrator"));
                options.AddPolicy("UserRole", policy =>
                    policy.RequireRole("User"));
            });

            services.AddControllersWithViews();
            services.AddRazorPages();
        }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }


[Authorize(Policy = "AdministratorRole")]
    public class InGameModel : PageModel
    {
public void OnGet() {

        }
public void onPost() {

        }
}

61c0e34e028c1273802562.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan_Worker, 2021-12-20
@Ivan_Worker

I found the answer. If anyone is interested, then catch the link https://stackoverflow.com/questions/29285406/refre...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question