EDIT: At the bottom .
I've this site which uses a WebService. Authentication is enabled using this way : "How to: Implement Simple Forms Authentication".
I've a WinForms application who cant connect to the WebService (because this one needs an authentication). There is an example how to perform this authentication between the WebService and the WinForms ? I heard about System.Net.HttpWebRequest, but I didn't find an example to use it .
class CookieWebClient : WebClient
{
public CookieContainer CookieContainer { get; private set; }
/// <summary>
/// This will instanciate an internal CookieContainer.
/// </summary>
public CookieWebClient()
{
this.CookieContainer = new CookieContainer();
}
/// <summary>
/// Use this if you want to control the CookieContainer outside this class.
/// </summary>
public CookieWebClient(CookieContainer cookieContainer)
{
this.CookieContainer = cookieContainer;
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address) as HttpWebRequest;
if (request == null) return base.GetWebRequest(address);
request.CookieContainer = CookieContainer;
return request;
}
}
and when I want to authenticate myself :
using (var client = new CookieWebClient())
{
var values = new NameValueCollection
{
{ "UserEmail.Text", "Administrator" },
{ "UserPass.Text", "admin" },
};
client.UploadValues("http://localhost:54787/logon.aspx", values);
// If the previous call succeeded we now have a valid authentication cookie
// so we could download the protected page
string result = client.DownloadString("http://localhost:54787/MyWantedPage.aspx");
}
The problem is maybe, that I don't really put the right user and password (Administrator/admin) in the right field on the Logon.aspx... (UserMail and UserPass).
What do you thing ? (of course I assume is easier to use WebClient class.. ) (If I really got it, NetworkCredentials is only available with Windows Authentication and Basic Authenticaiton isnt ? I've in my side, FormsAuthentication - the same one on the URL I gave)
EDIT :
OK, I saw here :
http://odetocode.com/articles/162.aspx
And so, I also tried to perform the HTTPWebRequest saw it on the page.
I get this error when closing the response :
webRequest.GetResponse().Close();
the error is below :
System.Net.WebException: Le serveur distant a retourné une erreur : (500) Erreur interne du serveur.
à System.Net.HttpWebRequest.GetResponse()
Apparently there is really a problem with my WebClient POST request and my HTTPWebRequest, may be something to configure on the IIS server ?
Thanks again the community !
OK, I don't really find an answer about that.
The way I do it is : protect all the Web pages using Forms Authentication. But not protect the Service I wnat to consume... (because yes, that was the goal of all of this ! )
Thank you very much anyway to all of you.