Blog de Francisco Velázquez

otro blog personal

Consumir un WebService con autenticación básica desde .NET

leave a comment »

El título describe bastante bien lo que vamos a solucionar en este post.

public class SecureWebService : ThirdPartyWebService
{
    protected override WebRequest GetWebRequest(Uri uri)
    {
        HttpWebRequest webRequest = (HttpWebRequest)base.GetWebRequest(uri);
        NetworkCredential credentials = Credentials as NetworkCredential;
        if (credentials != null)
        {
            string authInfo =
            ((credentials.Domain != null) && (credentials.Domain.Length > 0) ?
            credentials.Domain + @"\" : string.Empty) +
            credentials.UserName + ":" + credentials.Password;
            authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
            webRequest.Headers["Authorization"] = "Basic " + authInfo;
        }
        return webRequest;
    }
}

via.

Written by fravelgue

October 5, 2010 at 12:38 pm

Posted in .net

Tagged with , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: