webservice/public/code/authentication-csharp.txt

52 lines
1.6 KiB
Plaintext
Raw Normal View History

2011-06-16 14:55:30 +00:00
/*
Ajouter votre référence web puis ajouter cette fonction dans la référence
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest request;
request = (HttpWebRequest)base.GetWebRequest(uri);
if (PreAuthenticate)
{
NetworkCredential networkCredentials = Credentials.GetCredential(uri, "Basic");
if (networkCredentials != null)
{
byte[] credentialBuffer = new UTF8Encoding().GetBytes(networkCredentials.UserName + ':' + networkCredentials.Password);
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
}
else
{
throw new ApplicationException("No network credentials");
}
}
return request;
}
*/
string login = "";
string password = "";
// Create a new instance of the MD5CryptoServiceProvider object.
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(login + "|" + password));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
string hash = sBuilder.ToString();
EntrepriseService service = new EntrepriseService();
NetworkCredential Auth = new NetworkCredential(login, hash);
Uri uri = new Uri(service.Url);
ICredentials credentials = Auth.GetCredential(uri, "Basic");
service.Credentials = credentials;
service.PreAuthenticate = true;