title: Asp.NET MVC cookie not persisting data when accessed remotely
body: <p>I have an issue with my asp.net MVC project, I am using cookies to persist  user’s data
  I use the following code to set cookie after successful login:</p>
  
  <pre><code>[HttpPost]
      public ActionResult Index(string username,string password)
      {
  
          User user = db.Users.Where(t =&gt; t.username == username &amp;&amp; t.password == password).SingleOrDefault();
          if (user != null)
          {
  
              HttpCookie aCookie = new HttpCookie("cookie");
              aCookie.Values["username"] = username;
              aCookie.Values["role"] = user.role.ToString();
              aCookie.Values["UserID"] = user.UserID.ToString();
              aCookie.Values["route"] = "AdminReports";
              aCookie.Secure = false;
              aCookie.Expires = DateTime.Now.AddDays(1);
              Response.Cookies.Add(aCookie);
  
              isLogedIn = true;
              return RedirectToAction("AdminReports", "Home");
          }
          TempData["ErrorMessage"] = "Wrong username or password!";
          return View();
      }
  </code></pre>
  
  <p>I Read the cookie using this code :</p>
  
  <pre><code>public ActionResult AdminReports()
      {
  
  
  
  
          Response.Write(Server.HtmlEncode(Request.Cookies["cookie"]["username"]));
         // Response.Write(Request.Cookies["cookie"]["username"]);
  
  
          if (Request.Cookies["cookie"] != null)
          {
              if (Convert.ToInt32(Request.Cookies["cookie"]["role"]) == (int)Enums.Role.Admin)
              {
  
                  return View();
              }
              else if (Convert.ToInt32(Request.Cookies["cookie"]["role"]) == (int)Enums.Role.The70Hospitals)
              {
  
                  return View("The70Hospitals");
              }
              else if (Convert.ToInt32(Request.Cookies["cookie"]["role"]) == (int)Enums.Role.The380Hospitals)
              {
  
                  return View("The380Hospitals");
              }
              else
              {                  
                  return View("LoginView");
              }
          }
          else
          {
              return View("LoginView");
          }
      }
  </code></pre>
  
  <p>However the cookies lose its data which prevent the user to login. This case happens when I access the project remotely. However it works fine locally in the development mode and it runs normally also when I browse from the IIS (Run locally in the server)</p>