Request.IsAuthenticated 不会更改 ASP.NET MVC 中 FormsAuthentication 的视图

Request.IsAuthenticated doesn't change the view in FormsAuthentication in ASP.NET MVC

我正在开发一个简单的 asp.Net MVC 应用程序,它需要 FormsAuthentication,

型号

public class Member
{
    [Required]
    [Display(Name = "Username")]
    public string Username { set; get; }

    [Required]
    [Display(Name = "Password")]
    public string Password { set; get; }

    [Display(Name = "Remember Me?")]
    public bool RemeberMe { set; get; }

    public bool IsValid(string username,string password)
    {
        return (new TestdbEntities()).Members.Any(m => m.Username == username && m.Password == password);
    }
}

控制器

[HttpGet]
public ActionResult Login()
{
    return View();
}

[HttpPost]
public ActionResult Login(Models.Member member)
{
    if (ModelState.IsValid)
    {
        if (member.IsValid(member.Username, member.Password))
        {
            FormsAuthentication.SetAuthCookie(member.Username,member.RemeberMe);
            return RedirectToAction("Index","Home");
        }
        else
        {
            ModelState.AddModelError("","Invalid Username/Passowrd!");
        }
    }
    return View(member);
}

查看

问题出现在视图中,我希望当用户成功验证时登录link更改为注销link,但即使我跟踪,登录也成功但是Request.IsAuthenticated 是假的。

<body>
    <ul class="nav nav-pills">
        <li>
            @Html.ActionLink("Home", "Index", "Home")
        </li>
        <li>
            @if (Request.IsAuthenticated)
            {
                <label>Welcome </label>  @Html.Encode(User.Identity.Name)
                @Html.ActionLink("Signout", "Logout", "Membership")
                @Html.Label(User.Identity.Name.ToString())
            }
            else
            {
                @Html.ActionLink("Login", "Login", "Membership")
            }
        </li>
    </ul>
    <div>
        @RenderBody()
    </div>
</body> 

使用以下内容:

User.Identity.IsAuthenticated()

检查你的web.config文件,你必须在<system.web>标签下添加<authentication mode="Forms"/>