[英]User.Identity.IsAuthenticated returns false after SignInAsync invoked
我有一個彈出表單,我使用jquery UI對話框加載我的登錄視圖。單擊登錄按鈕時,我使用ajax post調用Account / Login操作。這是我的代碼:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
//IsAuthenticated is returns false
bool isAuthenticated= User.Identity.IsAuthenticated;
return PartialView("_LoginPartial");
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
調用SignInAsync方法后,User.Identity.IsAuthenticated返回false,這是為什么呢?
將不勝感激提供的任何幫助。 讓我知道您是否需要其他信息來幫助回答這個問題
這是因為asp.net管道尚未對用戶進行身份驗證。
User.Identity.IsAuthenticated
值基於HttpContext.Current。 應該發生的是SignInAsync
將添加一個cookie,框架將使用該cookie對未來的請求進行身份驗證。
因此,在擊中該Controller動作之后。 User.Identity.IsAuthenticated
在所有控制器操作中都為true。 AuthorizeAttribute
基於此。
要獲取用戶名,我將查看UserManager.FindAsync
返回的用戶屬性,即
var userName = user.UserName
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.