开发者

System.Net.CredentialCache and HttpWebResponse

I'm trying coding some functionality where the user may log in into a remote server by using its own Windows Credentials or by specifying some user, password and domain.

In order to know how to do it I read this link[1].

I have been able to successfully log in via CredentialCache.DefaultCredentials.

However, whenever I try to authenticate via user, name, password and domain I keep on getting a 401 error.

After some Googling and searching here I have found some probable errors (redirecting, different auth. types {basic, digest, ntlm, negotiate} and even the case contrary [i.e. being able to login through user+pasword but no by CredentialCache.DefaultCredentials]).

Any hints?

Edit: maybe some code would give you some clues about what I am doing wrong.

static void Main(string[] args)
{
    string password = "password", username = "Username", dom = "DOMAIN";
    string url = "http://my.url.com/LoginWithNativeCredentials?";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

    /////// Different User code////
    NetworkCredential credentials = new NetworkCredential(username, password, dom);
    CredentialCache cache = new CredentialCache();
    cache.Add(new Uri(url), "NTLM", credentials);
    request.Credentials = cache;
    /////////////////////////

    //////开发者_如何学编程 Current Windows user's credential
    //request.Credentials = CredentialCache.DefaultCredentials;
    /////////////////////////

    request.AllowAutoRedirect = true;
    request.CookieContainer = new CookieContainer(5);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {

        Console.WriteLine("In!");
    }

    Console.WriteLine("Done!");
    Console.ReadLine();
}

Many thanks!

[1] http://support.microsoft.com/kb/811318

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜