CookieAuthenticationを使用して、自分の.netコアサイトの現在のユーザーを認証しようとしています。 ログインした後、私はどんなURLにもリダイレクトされていません。私はまだログインフォームにいます。デバッグするとき、私は私の "authtorized"コントローラに移動すると、私のUserがまだ認証されていないことを知り、私は '302 found'(?)を取得します。ASP.NETコアでのCookieによる認証
私は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 マイ保護コントローラ(私はスキームを指定する必要がある場合はわからないイム)
[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);
}