dimanche 28 juin 2015

Request.IsAuthenticated doest change the view in FormsAuthentication in ASP.NET MVC

I am developing simple asp.Net MVC application which needs FormsAuthentication,

Model

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);
    }
}

Controller

    [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);
    }

View

The problem occure in view, which i expect the login changed to Logout when the user successfully authenticated, but even when i trace, the login is successful but Request.IsAuthenticated is false.

<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> 

Aucun commentaire:

Enregistrer un commentaire