I have a Windows application which needs to call a Forms Authenticated MVC action. The problem is, I am not sure how to authenticate a call from a Windows application into a Forms Authenticated MVC application.
The following code returns the login page. Is there a way to allow the below code to work with Forms Authentication by whatever means, or do I need to create a new Web API project which uses Basic Authentication for this to work?
WebRequest req = WebRequest.Create("http://myurl/protectaction");
req.Credentials = new NetworkCredential("username", "password");
var res = req.GetResponse();
Thank you very much for your help,
Richard Hughes
You could use a CookieContainer to store the FormsAuthentication cookie that is emitted by the LogOn action. So basically you will send a first request to the LogOn action and then reuse the same WebRequest instance for subsequent requests.
This way you will be sending your credentials over the wire only once.
For example: