我在VS 2013中使用默認的MVC 5項目。在_LoginPartial.cshtml中,它對Request.IsAuthenitcated執行檢查並分支它返回的內容。我的問題是在哪裏設置?我修改了我的代碼,以便能夠使用Google直接從主頁登錄,但是在它完成並返回索引後,此IsAuthenticated值仍然是錯誤的。ASP.NET身份以及Request.IsAuthenticated如何設置
我做了一個「使用Google登錄」鏈接,點擊後將其指向現有的ExternalLogin()動作。之後,驗證谷歌它調用ExternalLoginCallback()稍微修改,以自動創建一個GUID用戶名,然後登錄。但是,IsAuthenticated仍然是錯誤的。我錯過了什麼?
代碼確實到達了ExternalLoginCallback()內部的SignInAsync(),並且沒有發生錯誤,並且它返回索引就好了,所以不知道有什麼問題。
// POST: /Account/ExternalLogin
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
// Request a redirect to the external login provider
return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}
//
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
// Sign in the user with this external login provider if the user already has a login
var user = await UserManager.FindAsync(loginInfo.Login);
if (user != null)
{
await SignInAsync(user, isPersistent: false);
return RedirectToLocal(returnUrl);
}
else
{
// Get the information about the user from the external login provider
var info = await AuthenticationManager.GetExternalLoginInfoAsync();
if (info == null)
{
return View("ExternalLoginFailure");
}
//var user = new ApplicationUser() { UserName = model.UserName };
user = new ApplicationUser() { UserName = Guid.NewGuid().ToString().Replace("-", "") };
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
result = await UserManager.AddLoginAsync(user.Id, info.Login);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToLocal(returnUrl);
}
}
AddErrors(result);
}
return View();
}
您是否嘗試清除Cookie? –