【问题标题】:ASP.NET Identity and how Request.IsAuthenticated gets setASP.NET 身份以及如何设置 Request.IsAuthenticated
【发布时间】: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


【解决方案1】:

SignInAsync 会导致创建 cookie,然后框架会在加载页面时读取该 cookie 以设置 IsAuthenticated。

很可能,您的 cookie 已损坏,导致它们无法正确设置。清除 cookie 可能会解决此问题。

【讨论】:

  • 清除了 cookie。我知道它有效,因为当它去谷歌进行我的身份验证时,我必须输入我的电子邮件和密码,因为它都被清除了。当它回来时,我没有收到任何错误,并且 Request.IsAuthenticated 在索引页面上仍然是错误的。
猜你喜欢
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
相关资源
最近更新 更多