私はASP.NET Coreでアプリケーションを開発しており、カスタムCookie認証を使用しています。私CookieAuthenticationOptions
は以下のとおりです。ASP.NETコア認証Cookieは一度だけ受信されました
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,
LoginPath = new PathString("/login"),
AccessDeniedPath = new PathString("/unauthorized/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
クッキーがうまく作成され、私は、アプリケーションを実行している全体の時間を通して、ブラウザの設定でそれを見ることができます。これは私のHomeController
クラスです:
public HomeController(IHostingEnvironment env,
IAntiforgery antiforgery,
IOptions<AppSettings> appSettings,
TerminalDbContext terminalContext,
ILoggerFactory loggerFactory,
IHttpContextAccessor _httpContextAccessor)
{
_env = env;
_antiforgery = antiforgery;
_appSettings = appSettings;
_terminalContext = terminalContext;
_logger = loggerFactory.CreateLogger<HomeController>();
_httpContext = _httpContextAccessor.HttpContext;
_logger.LogInformation("Cookie coming");
var cookies = _httpContext.Request.Cookies[".AspNetCore.Cookies"];
if (cookies != null)
{
_logger.LogInformation(cookies.Length.ToString());
_logger.LogInformation(cookies.ToString());
}
else
{
_logger.LogInformation("THE COOKIE IS NULL");
}
}
そして、これは私がユーザーに署名する方法です:
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, loginInfo.Username),
new Claim("DbName", loginInfo.Terminal.SesamDbName),
};
var userIdentity = new ClaimsIdentity(claims, "password");
ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
await _httpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
私はHttpGet
を持っているので、私は、アプリケーションを実行しているとHomeController
の複数のインスタンスが作成されますビューに必要なJsonResult
を返すメソッド。
アプリケーションが初めて[Authorize]
(Index()
メソッドの場合)に試行されると、Cookieが検出され、認証され、正常に認証されます。 2回目に[Authorize]
(HttpGet
メソッドを返すと、JsonResult
が返されます)、ブラウザの設定にもかかわらず、Cookieが見つかりません。これを説明するためのログです:
...
info: Server.Controllers.HomeController[0]
Cookie coming
info: Server.Controllers.HomeController[0]
347
info: Server.Controllers.HomeController[0]
CfDJ8GSLZENXaNpNrtmz2DAt9joqJ6CEHpCFbJdbNxbQYjjoQmd4naOI0L0krNMSQdVhqPRP9tJJMMIRayc5ILRQMcJQWNZ0T9Fjuk7Qxg65wPP7SR43UZxwy6vGQ7_qeSp44gYLLe4NGEalhXynZxmD-jywqL4VJZ5y4OwpsEKLx-VVT03xAlt54J_qQk_O4wjwLQiZBpAVTFKUWN4u7H8yd_rwMTIGBPu21t5n35To9bTQU5677xNxiEFap3ukuxO4p-OxVakXqShy2Xk_vYDAvv_XFV6jgNcy4ZiCRB8VUhXGcNr205h4X0-O7JHB8mYbc13aZLmrAwvG5DWTBd3_OCo
...
info: Server.Controllers.HomeController[0]
Cookie coming
info: Server.Controllers.HomeController[0]
THE COOKIE IS NULL
なぜこれが起こりますか?それについて私は何ができますか?
本当にあなたはHTTPS経由でリクエストを行っていますか? –
どうすれば確認できますか? –
私は問題が何であるかを理解しました。助けてくれてありがとう、私はすぐに答えを投稿します。 –