I have a problem with my Request.IsAuthenticated always returns false. I install 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 should display login or logout.
@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 on the Internet, I created a class with a value of bool and tried to use this class. However, I get that the object is not set to the exception instance of the new variable. Here's how I created it: // 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 I initialize the class in the controller
public ActionResult Login()
{
ViewModelAuthenticate auth = new ViewModelAuthenticate();
auth.IsAuthenticate = false;
return View();
}
// , . , , .
? .
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" slidingExpiration="true" />
</authentication>