I am having an issue with my Request.IsAuthenticated always return false. I am setting the AuthCookie
CurrentRequest currentRequest = null;
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
} else if (login.ValidateUser(acct.UserName, acct.Password))
{
FormsAuthentication.SetAuthCookie(acct.UserName, true); //Edit on 11/12 @11:08
currentRequest = new CurrentRequest();
SessionWrapper.currentRequest = currentRequest;
return RedirectToAction("About", "Home");
}
//This is a partial login page that is supposed to display login or Logoff.
@using KMSS.Helper;
// this is always false
@if (Request.IsAuthenticated) //Same issue with User.Identity.IsAuthenticated
{
if (SessionWrapper.currentRequest != null)
{
<text> Welcome <strong> @SessionWrapper.currentRequest.Username </strong>
[@Html.ActionLink("Sign Off", "Logoff", "Account")]
</text>
} else {
@: [ @Html.ActionLink("Sign In", "Login", "Account") ]
}
} else
{
@:[ @Html.ActionLink("Sign In", "Login", "Account") ]
}
After reading online, I created a class with a bool value and tries to use that class instead. However, I am getting the object is not set to instance of a new variable exception. Here is how I had it set up: //Partial Login page
@model KMSS.Helper.ViewModelAuthenticate;
// this is always false
@if (Model.IsAuthenticated)
//The model is null even though I create create a reference in the Login Method i.e.
(ViewModelAuthenticate auth = new ViewModelAuthenticate();
{
if (SessionWrapper.currentRequest != null)
{
<text> Welcome <strong> @SessionWrapper.currentRequest.Username </strong>
[@Html.ActionLink("Sign Off", "Logoff", "Account")]
</text>
} else {
@: [ @Html.ActionLink("Sign In", "Login", "Account") ]
}
} else
{
@:[ @Html.ActionLink("Sign In", "Login", "Account") ]
}
//Here is the class public class ViewModelAuthenticate { public bool IsAuthenticate { get; set; } }
//Here is where I am initializing the class in the controller
public ActionResult Login()
{
ViewModelAuthenticate auth = new ViewModelAuthenticate();
auth.IsAuthenticate = false;
return View();
}
//I tried this inside and outside of Login, and it is called before the partial login view. However, I am still getting the object is not set to instance of a new variable exception. What am I doing wrong here? Your help will be appreciated.
//Showing the authentication section of the config file.
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" slidingExpiration="true" />
</authentication>
authentication
element of your web.config is correct ? Could you post it ? – Receiptor