Request.IsAuthenticated Return False all the time

前端 未结 3 1533
[愿得一人]
[愿得一人] 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: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){
       It WORKS!!
    }else{
       Nope, still not working
    }
    

提交回复
热议问题