Why does not the authorization ASP.NET Core?

Want to do authorization by roles as here.
In the controller AdminController
[Authorize(Roles = "admin")]
 public IActionResult Index()
{
 return View();
 }

after login throws back to the Login page, authorization. If you remove the [Authorize(Roles = "admin")] it runs the Index action of the controller AdminController. Why it does not works. In ClaimTypes.Name and Role record is added, check. Cookies are allowed. After Submit set cookies
Startup.cs

public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
 .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
 options.LoginPath = new PathString("/Auth/Login");
 options.AccessDeniedPath = new PathString("/Auth/AccessDen");
});
services.AddSingleton<IConfiguration>(Configuration);
services.AddDistributedMemoryCache();
 services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
 services.AddSession(opts =>
{
 opts.Cookie.IsEssential = true; // make the session cookie Essential
});
services.AddControllersWithViews();
}

 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.UseCookiePolicy();
app.UseSession();
app.UseDeveloperExceptionPage();

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


 app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
 name: "default",
 pattern: "{controller=Auth}/{action=Login}/{id?}");
});
 }



AuthController

[HttpPost]
[ValidateAntiForgeryToken]
 public async Task<IActionResult> Login(string iin,string pas)
 if (iin != null && pas!= null)
{
 Users Users = await reposUsers.FindUser(iin, pas); //Use PostgreSQL+Dapper
 if (users != null)
{
 await Authenticate(iin, users.role);
 return RedirectToAction("Index", "Admin");
}
}
}
private async Task Authenticate(string iin,string role)
{
 // create a single claim
 var claims = new List<Claim>
{
 new Claim(ClaimTypes.Name, iin)
 new Claim(ClaimTypes.Role, Role),
};
 // create a ClaimsIdentity object
 ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType,
ClaimsIdentity.DefaultRoleClaimType);
 // setting the authentication cookie
 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id));
 }
April 19th 20 at 12:01
0 answer

Find more questions by tags ASP.NET