Request.IsAuthenticated返回假所有的时间 [英] Request.IsAuthenticated Return False all the time
问题描述
我有我的Request.IsAuthenticated一个问题总是返回false。我设置AuthCookie
CurrentRequest currentRequest = NULL; 如果(Url.IsLocalUrl(RETURNURL)及&放大器; returnUrl.Length→1&放大器;&放大器; returnUrl.StartsWith(/)
&功放;&安培; !returnUrl.StartsWith(//)及与放大器; !returnUrl.StartsWith(/ \\\\))
{
返回重定向(RETURNURL);
}否则如果(login.ValidateUser(acct.UserName,acct.Password))
{
FormsAuthentication.SetAuthCookie(acct.UserName,真); //编辑在11/12 @ 11:08
currentRequest =新CurrentRequest();
SessionWrapper.currentRequest = currentRequest;
返回RedirectToAction(关于,家);
}
//这是应该显示登录或注销的部分登录页面。
@using KMSS.Helper;
// 这始终是假的
块引用>@if(Request.IsAuthenticated)//与User.Identity.IsAuthenticated同样的问题
{
如果(SessionWrapper.currentRequest!= NULL)
{
<文字和GT;欢迎<强> @ SessionWrapper.currentRequest.Username< / STRONG>
[@ Html.ActionLink(搁笔,注销,帐户)]
< /文字和GT;
}其他{
@:@ Html.ActionLink(登录,登录,帐户)]
}
}其他
{ @:@ Html.ActionLink(登录,登录,帐户)]
}在网上看完后,我创建了一个类与布尔值,并尝试使用类代替。然而,我正在对象没有被设置为一个新的变量异常的实例。
这是我怎么过的设置:
//局部登录页@model KMSS.Helper.ViewModelAuthenticate;
// 这始终是假的
块引用>@if(Model.IsAuthenticated)
//该模型为空,即使我创建创建的登录方法,即参考
(ViewModelAuthenticate AUTH =新ViewModelAuthenticate();
{
如果(SessionWrapper.currentRequest!= NULL)
{
<文字和GT;欢迎<强> @ SessionWrapper.currentRequest.Username< / STRONG>
[@ Html.ActionLink(搁笔,注销,帐户)]
< /文字和GT;
}其他{
@:@ Html.ActionLink(登录,登录,帐户)]
}
}其他
{ @:@ Html.ActionLink(登录,登录,帐户)]
}//这里是类
公共类ViewModelAuthenticate
{
公共BOOL IsAuthenticate {搞定;组; }
}//这里是我在控制器初始化类
公众的ActionResult登录()
{
ViewModelAuthenticate AUTH =新ViewModelAuthenticate();
auth.IsAuthenticate = FALSE;
返回查看();
}//我想这里面并登录之外,它是局部登录视图之前调用。不过,我仍然得到对象未设置为一个新的变量异常实例。
我在做什么错在这里?您的帮助将是AP preciated。//显示配置文件的认证部分。 <身份验证模式=表格>
<形式loginUrl =〜/帐号/登录超时=2880slidingExpiration =真/>
< /认证>
解决方案我代替我的验证部分与此示例,一个样品,我发现的此处。现在正在工作。
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>
解决方案I replaced my authentication section with this sample that a sample that I found here. It is working now.
这篇关于Request.IsAuthenticated返回假所有的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!