locked
Validating Digest Authentication RRS feed

  • Question

  • Hi All,

    I am trying to connect to one URL using digest authentication (HttpWebRequest+NetworkCredential ). I need to validate the user name and password i.e need to check for wrong username and password. Here is the code, I have implemented. Even when I am giving wrong username and password. I am getting httpresponse status code OK means 

    System.Net.HttpStatusCode.OK

                    

                    Uri myUri = new Uri(URL);
                    httprequest = (HttpWebRequest)HttpWebRequest.Create(myUri);

                    NetworkCredential mynetworkcredential = new NetworkCredential(strUser, strPassword);
                    mycredentialcache = new CredentialCache();

                    mycredentialcache.Add(myUri, "Digest", mynetworkcredential);
                    httprequest.PreAuthenticate = true;
                    httprequest.Credentials = mycredentialcache;
                    httprequest.UserAgent = "MSIE";
                    httprequest.AllowAutoRedirect = true;
                    httprequest.KeepAlive = true;

                    HttpWebResponse httpresponse = (HttpWebResponse)httprequest.GetResponse();
                    System.Windows.MessageBox.Show(httpresponse.StatusCode.ToString());
                    System.Windows.MessageBox.Show(httpresponse.Server.ToString());

    What could be the problem ? Can anybody tell me how to validate wrong username and password ?

    Mrutyunjaya

    Monday, March 9, 2015 7:00 PM

Answers

  • Hi Mrutyunjaya,

    Please try to test the following code, it shows how to request a web page using the HttpWebRequest class with digest authentication method enabled.

    private string LoadHttpPageWithDigestAuthentication(string url, string username, string password)
    {
        Uri myUri = new Uri(url);
        WebRequest myWebRequest = HttpWebRequest.Create(myUri);
    
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
    
        NetworkCredential myNetworkCredential = new NetworkCredential(username, password);
    
        CredentialCache myCredentialCache = new CredentialCache();
        myCredentialCache.Add(myUri, "Digest", myNetworkCredential);
    
        myHttpWebRequest.PreAuthenticate = true;
        myHttpWebRequest.Credentials = myCredentialCache;
    
        WebResponse myWebResponse = myWebRequest.GetResponse();
    
        Stream responseStream = myWebResponse.GetResponseStream();
    
        StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
    
        string pageContent = myStreamReader.ReadToEnd();
    
        responseStream.Close();
    
        myWebResponse.Close();
    
        return pageContent;
    }

    In addition, we can validate a username and password in Active Directory

    // create a "principal c

    ontext" - e.g. your domain (could be machine, too) using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")) { // validate the credentials bool isValid = pc.ValidateCredentials("myuser", "mypassword"); }

    Hope it helpful for you.

    Good luck!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Tuesday, March 10, 2015 8:25 AM
    • Marked as answer by Kristin Xie Tuesday, March 17, 2015 9:56 AM
    • Unmarked as answer by Kristin Xie Wednesday, March 18, 2015 1:09 AM
    • Marked as answer by MRUTYUNJAYA.M Tuesday, August 28, 2018 5:04 AM
    Tuesday, March 10, 2015 7:44 AM