Im struggling to authenticate the current User for my .net core site using CookieAuthentication. After logging in i'm not being redirected to any url, and i'm still on the login form. When debugging i can see that my User is still not authenticated and i get a '302 found'(?) if i navigate to my "authtorized" controller.
I have the following setup in startup.cs.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
const int durationInSeconds = 60 * 60 * 24;
ctx.Context.Response.Headers[HeaderNames.CacheControl] =
"public,max-age=" + durationInSeconds;
}
});
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "myCustomScheme",
LoginPath = new PathString("/Account/Unauthorized/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true,
CookieSecure = env.IsDevelopment() ? CookieSecurePolicy.SameAsRequest : CookieSecurePolicy.Always
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
AdminController.cs My protected controller(im unsure if i need to specify the scheme)
[Authorize(ActiveAuthenticationSchemes = "myCustomScheme")]
public class AdminController : Controller
{
public IActionResult Index()
{
return View();
}
}
AccountController:
[HttpPost]
public async Task<IActionResult> Unauthorized(LoginModel model, string ReturnUrl)
{
if (ModelState.IsValid)
{
if (model.Username.ToLower() == "test" && model.Password == "test")
{
var principal = User as ClaimsPrincipal;
await HttpContext.Authentication.SignInAsync("myCustomScheme", principal, new AuthenticationProperties
{
IsPersistent = true,
});
return RedirectToAction(nameof(AdminController.Index));
}
return View(model);
}
return View(model);
}