SignInManager и фильтры авторизации
делаю небольшой учебный проект на ASP.NET MVC. Пытаюсь реализовать регистрацию/авторизацию через SignInManager и фильтр авторизации. Но после выполнения метода SignInManager.SignInAsync(user, isPersistent: false, default), доступа к action с фильтром все еще нет. Подскажите, что я делаю не так, пожалуйста.
AccountController:
public class AccountController : Controller
{
private SignInManager<IdentityUser> _signInManager;
private UserManager<IdentityUser> _userManager;
private ApplicationContext _context;
public AccountController(SignInManager<IdentityUser>signInManager,
UserManager<IdentityUser> userManager,
ApplicationContext context)
{
_signInManager = signInManager;
_userManager = userManager;
_context = context;
}
[HttpGet]
public IActionResult Registration()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Registration(UserVM userVM)
{
var findResult = await _userManager.FindByNameAsync(userVM.Username);
if (findResult != null)
return BadRequest(findResult);
IdentityUser user = new IdentityUser(userVM.Username);
var creationResult = await _userManager.CreateAsync(user, userVM.Password);
if (creationResult.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false, default);
return RedirectToAction("Index");
}
return RedirectToAction("Registration");
}
[Authorize]
public string Index()
{
return "Yes";
}
}
Startup.cs:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) => Configuration = configuration;
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
string connection = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<ApplicationContext>(options => options.UseSqlServer(connection));
services.AddIdentity<IdentityUser, IdentityRole>(opts =>
{
opts.Password.RequireNonAlphanumeric = false;
opts.Password.RequireLowercase = false;
opts.Password.RequireUppercase = false;
opts.Password.RequireDigit = false;
})
.AddEntityFrameworkStores<ApplicationContext>();
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute("CatchAll", "{controller}/{action}/{*data}");
});
}
}