Request.IsAuthenticated Return False all the time

不羁的心 提交于 2019-12-02 13:24:48

问题


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>

回答1:


I replaced my authentication section with this sample that a sample that I found here. It is working now.




回答2:


Looking at your code, I feel that there is more going on here than you are showing us. Specifically, the variables CurrentRequest and SessionWrapper, setting them to null on the beginning of the Action method call, etc. I would suggest trying a basic, bare bones example in your project and then begin to add items back in as needed. No AJAX, only full page post back to the server from your login form. Such an example would look like:

Login View Model

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

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

Login POST Action method

[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");
}

About View

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



回答3:


I was testing and I set my time back a few days. For someone reason it caused this issue after putting the date back it was fine. I assume windows forms had the old date (which was todays date) cached so I assume it was expired. Just a thought about the matter.



来源:https://stackoverflow.com/questions/19933430/request-isauthenticated-return-false-all-the-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!