2017-07-25 11 views
2

私は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 

なぜこれが起こりますか?それについて私は何ができますか?

+1

本当にあなたはHTTPS経由でリクエストを行っていますか? –

+0

どうすれば確認できますか? –

+0

私は問題が何であるかを理解しました。助けてくれてありがとう、私はすぐに答えを投稿します。 –

答えて

1

問題はバックエンドとは関係ありませんでした。私はリアクションをフロントエンドで使用していますが、問題はfetch()GETメソッドのバックエンドにクッキーを渡していないことでした。リクエストでCookieを送信するには、{ credentials: 'same-origin' }fetch()に設定するだけでした。すべての助けに感謝します。

関連する問題