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