ASP.Net MVC:Request.IsAuthenticated成功登录后变为false

时间:2016-09-20 15:00:49

标签: asp.net-mvc

我正在使用asp.net MVC和身份来验证用户身份。我在成功验证Request.IsAuthenticated return false

后看到了

这是示例登录代码,我调试并看到它正常工作。

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    // This doesn't count login failures towards account lockout
    // To enable password failures to trigger account lockout, change to shouldLockout: true
    var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(model);
    }
}

成功登录页面后重定向到主页索引操作,我在索引视图中显示用户信息,如下所示,但由于Request.IsAuthenticated return false

,控件永远不会出现
<div class="row">
    <div class="col-md-12">
        @{
            if (Request.IsAuthenticated)
            {
                var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                var currentUser = manager.FindById(User.Identity.GetUserId());
                <p>@currentUser.UserName</p>
                <p>@currentUser.BirthDate</p>
            }
            else
            {
                <p>not authenticated</p>
            }
        }

    </div>
</div>

只是不明白我错过了哪个请求验证方法总是返回false。请帮我修理一下。感谢

1 个答案:

答案 0 :(得分:0)

在HTTP下使用IISExpress运行应用程序时遇到了同样的问题。通过在项目属性中将SSL Enabled设置为true切换到HTTPS后,问题得以解决。