MVC how to call authorized action method from console application
<p>I need to trigger mvc methods from console application, all the methods are under authorize and need login.</p>

<p>after some searching I found this<br>
<a href="https://stackoverflow.com/questions/10036330/how-to-authenticate-in-an-asp-net-mvc-application-from-a-console-application">How to authenticate in an ASP.NET MVC application from a console application</a><br>
which is very similar to my problem,<br>
Someone there mentioned that before calling the wanted method, a login is needed, then using the authentication cookie to send a request to the wanted method.</p>

<p>I'm using forms authentication</p>

<pre><code>&lt;authentication mode="Forms"&gt;
  &lt;forms loginUrl="~/Account/Login" timeout="2880" /&gt;
&lt;/authentication&gt;
</code></pre>

<p>My code (on console application)</p>

<pre><code>  private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Get authentication cookie");

        var request = (HttpWebRequest)WebRequest.Create("http://localhost:50709/Account/Login");

        var postData = "UserEmail=Test@gmail.com&amp;Password=P@ssW0rd";
        var data = Encoding.ASCII.GetBytes(postData);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }



        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

    }
</code></pre>

<p>My login action</p>

<pre><code>   [HttpPost]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        // Lets first check if the Model is valid or not
        if (ModelState.IsValid)
        {
            using (var db = new MarketingLogsEntities())
            {
                bool userValid = db.RegisteredUsers.Any(x =&gt; x.UserEmail == model.UserEmail &amp;&amp; x.Password == model.Password);



                // User found in the database
                if (userValid)
                {

                    FormsAuthentication.SetAuthCookie(model.UserEmail, false);
                    if (Url.IsLocalUrl(returnUrl) &amp;&amp; returnUrl.Length &gt; 1 &amp;&amp; returnUrl.StartsWith("/")
                        &amp;&amp; !returnUrl.StartsWith("//") &amp;&amp; !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
        }

        return View(model);
    }
</code></pre>

<p>my response doesn't contain any cookie data inside (cookie returns empty)</p>

<p><a href="https://i.stack.imgur.com/Z3vnV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z3vnV.png" alt="enter image description here"></a></p>

<p>What am I doing wrong?<br>
Appreciate any help.</p>