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

ret*_*tif 4 asp.net asp.net-mvc forms-authentication

我想div只在用户登录时在视图中显示一些内容.

这就是我试图这样做的方式:

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

但是,Request.IsAuthenticated(和User.Identity.IsAuthenticated)是始终 true,即使是在最开始,之后我开始从Visual Studio的网站.显然,它让当用户登录Windows(因为User.Identity.Name返回我的Windows登录),但我需要它来检查用户是否通过网站进行了身份验证FormsAuthentication.

那是我的web.config:

<authentication mode="Forms">
  <forms loginUrl="/Account/Login" timeout="2880" />
</authentication>
Run Code Online (Sandbox Code Playgroud)

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

Tec*_*chy 6

如果您的意思是要检查用户是否已登录,请使用以下命令:

@if (User.Identity.IsAuthenticated) { /* content */}
Run Code Online (Sandbox Code Playgroud)