発生している問題
asp.net core2のログイン認証を試しているのですが、
一度ログインするとブラウザを閉じて、開き直してもログイン状態が継続していまいます。
ブラウザを閉じて開き直したら再度ログイン認証をさせたいです。
補足情報
ログイン認証はデフォルトの自動生成されたログイン認証ではなく、カスタムでの認証を行っています。
ブラウザからクッキーの情報を確認すると、
Expiresには「1969-12-31T23:59:59.000Z」の値が設定されています。
プログラム
Startup.cs
C#
1 public void ConfigureServices(IServiceCollection services) 2 { 3 services.AddAuthentication("MyCookieAuthenticationScheme") 4 .AddCookie("MyCookieAuthenticationScheme", options => 5 { 6 options.LoginPath = "/Login"; 7 }); 8 services.AddDistributedMemoryCache(); 9 10 services.AddSession(options => 11 { 12 options.IdleTimeout = TimeSpan.FromSeconds(10); 13 options.Cookie.HttpOnly = true; 14 }); 15 } 16 17 public void Configure(IApplicationBuilder app, IHostingEnvironment env) 18 { 19 if (env.IsDevelopment()) 20 { 21 app.UseBrowserLink(); 22 app.UseDeveloperExceptionPage(); 23 } 24 else 25 { 26 app.UseExceptionHandler("/Error"); 27 } 28 app.UseStaticFiles(); 29 app.UseAuthentication(); 30 app.UseSession(); 31 app.UseMvc(); 32 } 33 }
Login.chtml.cs
C#
1 public async Task<IActionResult> OnPost() 2 { 3 //const string badUserNameOrPasswordMessage = "Username or password is incorrect."; 4 if (user == null) 5 { 6 return Page(); 7 } 8 9 if (user.UserName != "aa" || user.Password != "bb") 10 { 11 return Page(); 12 } 13 14 // Cookies 認証スキームで新しい ClaimsIdentity を作成し、ユーザー名を追加します。 15 var identity = new ClaimsIdentity("MyCookieAuthenticationScheme"); 16 identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName)); 17 18 // クッキー認証スキームと、上の数行で作成されたIDから作成された新しい ClaimsPrincipal を渡します。 19 await HttpContext.SignInAsync("MyCookieAuthenticationScheme", new ClaimsPrincipal(identity), 20 new AuthenticationProperties 21 { 22 IsPersistent = false 23 }); 24 return RedirectToPage("/Index"); 25 }
試したこと
AuthenticationPropertiesのIsPersistentをfalseにすれば永続化されないかと思い試しましたがだめでした。
またAuthenticationPropertiesのExpiresUtcに現在時刻を入れてみたのですが、ログイン自体ができなくなってしまいました。
言語/FW/ツール等のバージョンなど
言語:C#
フレームワーク:.Net Core2.0 razor page
OS:windows10

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/12 08:28
退会済みユーザー
2018/11/12 08:49
2018/11/15 08:29