1 Replies - 9087 Views - Last Post: 21 November 2014 - 06:53 AM

#1 Ghandi_Of_Codez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-March 13

Issue with Request.IsAuthenticated after Login page

Posted 16 February 2014 - 08:14 PM

I am having an issue with checking the authentication when a user logs in to my site. So I have a login page (Login.cshtml), that of course a user would login from. From there the user would be sent to an index page. My issue is that when i hit this

@if (Request.IsAuthenticated)
{
    <strong>@Html.Encode(User.Identity.Name)</strong>
}
else
{
    <strong>Something went wrong</strong>
}



it fails into the else statement and writes something went wrong. Any advice on how to combat this error would be greatly appreciated. Thank you in advance.

Index.cshtml:
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

@if (Request.IsAuthenticated)
{
    <strong>@Html.Encode(User.Identity.Name)</strong>
}
else
{
    <strong>Something went wrong</strong>
}



My UserContoller(has all the methods for the login):

public ActionResult Index()
        {
            var user = db.User.Include(u => u.UserName);
            var loggedInUser = User.Identity.Name;
            return View();
        }

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

        [HttpPost]
        public ActionResult Login(Models.User user)
        {
            if (ModelState.IsValid)
            {
                if (isValid(user.UserName, user.Password))
                {
                    FormsAuthentication.SetAuthCookie(user.UserName, false);
                    return RedirectToAction("Index", "User");
                }
                else
                {
                    ModelState.AddModelError("", "User Name or Password is incorrect");
                }
            }

            return View(user);
        }

        private bool isValid(string UserName, string Password)
        {
            bool isValid = false;

            //var user = db.User.SingleOrDefault(u => u.UserName == UserName);
            var user = db.User.Where(u => u.UserName == UserName).FirstOrDefault();
            var pass = db.User.Where(u => u.Password == Password).FirstOrDefault();

            if (user != null && pass != null)
            {
                isValid = true;
            }

            return isValid;
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Issue with Request.IsAuthenticated after Login page

#2 cllyldrm   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 62
  • Joined: 03-May 13

Re: Issue with Request.IsAuthenticated after Login page

Posted 21 November 2014 - 06:53 AM

Did you added in webconfig ?

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1