【发布时间】:2013-11-12 15:39:42
【问题描述】:
我的 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();
}
//我在Login内外都试过这个,在部分登录视图之前调用。但是,我仍然得到对象未设置为新变量异常的实例。 我在这里做错了什么?您的帮助将不胜感激。
//Showing the authentication section of the config file.
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" slidingExpiration="true" />
</authentication>
【问题讨论】:
-
不是答案,但您是否真的在验证登录之前设置了身份验证cookie?
-
@Tommy,谢谢。我正在移动并尝试一些东西,但我忘记将它移回。
-
您是否检查过您的 web.config 的
authentication元素是否正确?可以发一下吗? -
@jbl,我编辑了问题并添加了我的 web.config 文件的身份验证部分。
标签: asp.net-mvc-3 asp.net-mvc-4 telerik-mvc