Search code examples
asp.net-mvc-5form-authentication

How to display email after login in he masterpage


My model is

public class Login
{
    [Required]
    [Display(Name = "Email")]
    [EmailAddress]
    public string Email { get; set; }

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


}

My Post method is

    [HttpPost]
    public ActionResult Login(Models.Login lu)
    {
        using (travelAndTourismEntities objentity = new travelAndTourismEntities())
        {
            if (ModelState.IsValid)
            {
                if (IsValid(lu.Email, lu.Password))
                {
                    FormsAuthentication.SetAuthCookie(lu.Email, true);
                    return RedirectToAction("Home", "jettravel");
                }
                else
                {
                    ModelState.AddModelError("", "Login details are wrong.");
                }
            }
            return View(lu);
        }
    }

In view

@if (Request.IsAuthenticated)
{
     <div class="col-md-3 col-sm-3 col-xs-12" style=" font-size:14px; ">
     <div class="header_info">
     <p class="login_username">Welcome <span>@Html.Encode(User.Identity.Name)</span></p>
}

I want to display customer email in view, but @if (Request.IsAuthenticated) is always getting false


Solution

  • Try replacing this line

    <p class="login_username">Welcome <span>@Html.Encode(User.Identity.Name)</span></p>
    

    with

    <p class="login_username">Welcome <span>@HttpContext.Current.User.Identity.Name;</span></p>