Request.IsAuthenticated Return False all the time

I have a problem with my Request.IsAuthenticated always returns false. I install 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 should display login or logout.

@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 on the Internet, I created a class with a value of bool and tried to use this class. However, I get that the object is not set to the exception instance of the new variable. Here's how I created it: // 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 I initialize the class in the controller

 public ActionResult Login()
        {
           ViewModelAuthenticate auth = new ViewModelAuthenticate();
            auth.IsAuthenticate = false;
            return View();
        }

// , . , , . ? .

//Showing the authentication section of the config file.

 <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" slidingExpiration="true" />
    </authentication>
+1
3

, , . .

+2

, , , . , CurrentRequest SessionWrapper, Action .. , , 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>
}
+1

, . - , . , ( ), , . .

0
source

Source: https://habr.com/ru/post/1664716/


All Articles