Programmatic NTLM Web Request Authorization   

 
NTLM Web Request

The example below shows how to set the credentials of a web request to use ntlm authentication. I used this method to download files from a security trimmed SharePoint document library.

 Code Snippet

//Example on how to download a file using ntlm authentication
using (WebClient w = new WebClient())
{
    w.UseDefaultCredentials = false;                

    w.Credentials = GetCredentials(url, "username","password","domain");

    return w.DownloadData(url);                
} 

 Method to create the NTLM Credentials

/// 
/// Creates the credentials to be allowed to download a file from
/// a SharePoint document Library
/// 
/// Url for the credentials signature
/// CredentialCache
private CredentialCache GetCredentials(string url, string userName, string password, string domain)
{
    //Force Ntlm authentication
    AuthenticationManager.Unregister("Basic");

    AuthenticationManager.Unregister("Kerberos");

    //AuthenticationManager.Unregister("Ntlm");

    AuthenticationManager.Unregister("Negotiate");

    AuthenticationManager.Unregister("Digest");

    CredentialCache mycache = new CredentialCache();

    // Credentials are specified here

    NetworkCredential nw = new NetworkCredential(username, password, domainName);

    mycache.Add(new Uri(url), "Ntlm", nw);

    return mycache;
}
Published  on  2/3/2009  by  xsolon
0  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:          
Tags: | Categories:
Technorati Tags:
 

Links to this post

Comments

Title:
Receieve Notification:
Website:
Email:
Comments:

CAPTCHA Image Validation