【问题标题】:Request.IsAuthenticated Return False all the timeRequest.IsAuthenticated 一直返回 False
【发布时间】: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


【解决方案1】:

我用这个示例替换了我的身份验证部分,该示例是我找到的一个示例here。它现在正在工作。

【讨论】:

  • 我曾经遇到过同样的问题,但我认为你必须设置 FormsAuthentication.SetAuthCookie(acct.UserName, true);验证用户并确保在 web.config 中设置
  • 抓挠我的头 2 个小时,试图找出问题所在。才发现我没有在 web.config 中设置
  • 只是为了进一步澄清,这是添加到解决方案的 web.config 文件中,而不是视图文件夹中。您可以在&lt;system.web&gt; 标签中添加&lt;authentication mode="Forms"&gt;&lt;forms loginUrl="~/UserAdmin/LogIn" timeout="2880" /&gt;&lt;/authentication&gt;
【解决方案2】:

查看您的代码,我觉得这里发生的事情比您向我们展示的要多。具体来说,变量CurrentRequestSessionWrapper,在Action 方法调用开始时将它们设置为null,等等。我建议在您的项目中尝试一个基本的、简单的示例,然后根据需要开始重新添加项目.没有 AJAX,只有整个页面从您的登录表单回传到服务器。这样的示例如下所示:

登录视图模型

public class LoginViewModel{
   [Required]
   public string UserName {get;set;}

   [Required]
   public string Password {get;set;}
}

登录 POST 操作方法

[HttpPost]
public ActionResult Login(LoginViewModel model, string returnUrl){
   if(!ModelState.IsValid){
       return View();
   }
   if(!provider.ValidateUser(model.UserName, model.Password){
       ModelState.AddModelError("", "The username/password combination does not match");
       return View();
   }
   FormAuthentication.SetAuthCookie(model.UserName, true);
   if(!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl){
       return Redirect(returnUrl);
   }
   return RedirectToAction("About", "Home");
}

关于视图

@if(Request.IsAuthenticated){
   <b>It WORKS!!</b>
}else{
   <b>Nope, still not working</b>
}

【讨论】:

  • 我有一个 CurrentRequest 类型的对象,我将其包装在会话包装器中。当用户提交请求时,他们希望看到他们的一些请求信息在页面之间执行。我使用会话包装器在整个页面中携带该信息。我有一个继承 System.Web.Security.MembershipProvider 的类 KMSSAccountLogin,我用它来验证用户。我复制并粘贴了您的代码并进行了一些更改,以便验证用户。我运行了应用程序,消息总是:不,仍然无法正常工作。
  • 您的浏览器设置是否允许 cookie?如果您检查请求(在您登录后),您是否看到请求中包含 .ASPXAUTH cookie?这是在本地主机上还是在部署的服务器上?
  • 是的,请求中有一个 .ASPXAUTH。
【解决方案3】:

我正在测试,我把时间往后拨了几天。出于某种原因,它在将日期放回去后引起了这个问题,这很好。我假设 Windows 窗体缓存了旧日期(即今天的日期),所以我认为它已过期。只是想一想。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多