【发布时间】:2014-05-06 15:05:17
【问题描述】:
我在 VS 2013 中使用默认的 MVC 5 项目。在 _LoginPartial.cshtml 中,它会检查 Request.IsAuthenitcated 并分支它返回的内容。我的问题是在哪里设置?我修改了我的代码,以便能够使用 Google 直接从主页登录,但是在它这样做并返回到索引之后,这个 IsAuthenticated 值仍然是 false。
我创建了一个“使用 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();
}
【问题讨论】:
-
你试过清除你的cookies吗?
标签: asp.net authentication asp.net-mvc-5 asp.net-identity