我的 Request.IsAuthenticated 总是返回 false。我正在设置 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");
}
//这是一个部分登录页面,应该显示登录或注销。
@using KMSS.Helper;
//这总是假的
@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") ]
}
在线阅读后,我创建了一个具有布尔值的类,并尝试使用该类。但是,我得到的对象未设置为新变量异常的实例。这是我的设置方式://部分登录页面
@model KMSS.Helper.ViewModelAuthenticate;
//这总是假的
@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") ]
}
//这里是类 public class ViewModelAuthenticate { public bool IsAuthenticate { get; 放; } }
//这里是我在控制器中初始化类的地方
public ActionResult Login()
{
ViewModelAuthenticate auth = new ViewModelAuthenticate();
auth.IsAuthenticate = false;
return View();
}
//我在Login内外都试过这个,在部分登录视图之前调用 但是,我仍然得到对象未设置为新变量异常的实例。我在这里做错了什么?您的帮助将不胜感激。
//Showing the authentication section of the config file.
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" slidingExpiration="true" />
</authentication>