ASP.NET MVC FormsAuthentication 检查用户是否登录

ASP.NET MVC FormsAuthentication check if user logged in

我只想在用户登录后在视图中显示一些 div

我就是这样尝试的:

@{
    if (Request.IsAuthenticated)
    // if (User.Identity.IsAuthenticated)
    {
        <div>
            Some content only for logged in users.
        </div>
    }
}

但是 Request.IsAuthenticated(和 User.Identity.IsAuthenticated总是 true,即使是在最开始的时候,就在我从Visual Studio。显然,当用户登录 Windows 时,它会得到 me(因为 User.Identity.Name returns 我的 Windows 登录),但我需要它通过 FormsAuthentication.

检查用户是否已在网站上进行身份验证

那是我的 web.config:

<authentication mode="Forms">
  <forms loginUrl="/Account/Login" timeout="2880" />
</authentication>

如何检查用户是否已通过 FormsAuthentication 登录?

如果你的意思是要检查用户是否已经登录,我使用如下:

@if (User.Identity.IsAuthenticated) { /* content */}