Request.IsAuthenticated Return False all the time

前端 未结 3 1501
[愿得一人]
[愿得一人] 2021-01-24 01:37

I am having an issue with my Request.IsAuthenticated always return false. I am setting the AuthCookie

 CurrentRequest currentRequest = null;

            if (Url         


        
相关标签:
3条回答
  • 2021-01-24 02:01

    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.

    0 讨论(0)
  • 2021-01-24 02:12

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

    0 讨论(0)
  • 2021-01-24 02:13

    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>
    }
    
    0 讨论(0)
提交回复
热议问题