1
我正在構建使用Windows身份驗證的Intranet站點,但使用實體框架存儲授權聲明。沒有用戶登錄。如果瀏覽器關閉(cookie被刪除)並且應用程序重新啓動(CNTRL F5),Home/Index控制器會檢查用戶WindowsIdentity是否已註冊,並且應該登錄該用戶。ASP.NET核心SignInAsync需要瀏覽器刷新
public async Task<IActionResult> Index()
{
string userWin = WindowsIdentity.GetCurrent().Name.Split('\\')[1];
var user = await _userManager.FindByNameAsync(userWin);
if (user != null)
{
await _signInManager.SignInAsync(user, isPersistent: false);
string userHTTP = HttpContext.User.Identity.Name;
ViewData["Message"] = $"Registered User: {user} and HTTPUser: {userHTTP} is registered for this site";
}
else
ViewData["Message"] = "Please contact admin to register for this website";
return View();
}
第一次運行時,userHTTP爲空,並且需要用戶登錄的查看html不會執行。如果瀏覽器刷新,那麼一切正常。爲什麼登錄和HTTPContext需要刷新?