We're building a commandline application that needs some data from the IIS server using forms authentication. How can I do forms authentication from a commandline application? I've tried but all that happens is that the request gets redirected to the login page.
I'm guessing that if I could include an authentication cookie the request with the right credential, the download would be fine.
I have control over both client and server. I can set the the machineKey in web.config, but can't figure out how to set this in the commandline application. This has to be the same validationKey to encrypt the cookie in the right format?
The server is written in asp-net mvc.
var request = (HttpWebRequest)WebRequest.Create(uri);
var formsCookiePath = "/";
var cookieName = ".FormName";
var domain = "localhost";
var username = "username";
var password = "pa$$word";
var ticket = new FormsAuthenticationTicket(1,
username,
DateTime.Now,
DateTime.Today.AddYears(10),
true,
password,
formsCookiePath);
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var authenticationCookie = new Cookie(
cookieName,
encryptedTicket,
formsCookiePath,
domain)
{
HttpOnly = true,
Secure = false
};
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(authenticationCookie);
request.UserAgent = "Internal downloader";
var loWebResponse = (HttpWebResponse)request.GetResponse();
Update: Based on the answer from Zruty I used this example to generate the authentication cookie: