Home All Groups Group Topic Archive Search About

Issue in passing username and password credentials

Author
8 Jan 2007 5:16 AM
San
Hi,

I am facing a problem in posting Web request with username and password
credentials.
I am working on migrating Java client application to .Net which will send
request to Java servlet.

Java Client application code:
        HttpClient httpclient;
        httpclient = new HttpClient();
        UsernamePasswordCredentials usernamepasswordcredentials = new
UsernamePasswordCredentials(s, s1);
        httpclient.getState().setAuthenticationPreemptive(true);
        httpclient.getState().setCredentials(null, null,
usernamepasswordcredentials);
        Object obj = null;


I want info on .Net equivalent to Java's UsernamePasswordCredentials class .

This UsernamePasswordCredentials is different from .net's NetworkCredential.
I tried below code itz not working.

Please guide me on this issue.

Thanks
Santhosh



public static string HttpWebServiceRequest(string url, string userName,
string password, string xmlContent, string domain, int timeoutMS, int reqtype)
{
    try
    {
        System.Net.ServicePointManager.CertificatePolicy = new
TrustAllCertificatePolicy();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.Timeout = timeoutMS;
        request.ContentType = "text/xml";
        request.KeepAlive = true;
        request.ContentLength = xmlContent.Length;
        request.ProtocolVersion = HttpVersion.Version10;


        if((userName != "") && (password != ""))
        // I need to use some different class here.
            request.Credentials = new NetworkCredential(userName, password, domain);
            using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
            {
                sw.Write(xmlContent);
            }

        using (StreamReader sr = new
StreamReader(request.GetResponse().GetResponseStream()))
        {
            return sr.ReadToEnd();
        }

    }
    catch (Exception e)
    {
        throw;
    }
}

Author
8 Jan 2007 8:59 AM
Jakob Christensen
It looks like you are using SSL?

MSDN states that NetworkCredential does not work for SSL.  I have been
unable to find out what to do instead but you might want to try using this:

CredentialCache myCache = new CredentialCache();
NetworkCredential netCredential = new 
NetworkCredential(UserName,SecurelyStoredPassword,Domain);
myCache.Add(new Uri(url),"Basic", netCredential);
myCache.Add(new Uri(url),"Digest", netCredential);
myCache.Add(new Uri(url),"Negotiate", netCredential);
myRequest.Credentials = myCache;

HTH, Jakob.


Show quote
"San" wrote:

> Hi,
>
> I am facing a problem in posting Web request with username and password
> credentials.
> I am working on migrating Java client application to .Net which will send
> request to Java servlet.
>
> Java Client application code:
>         HttpClient httpclient;
>         httpclient = new HttpClient();
>         UsernamePasswordCredentials usernamepasswordcredentials = new
> UsernamePasswordCredentials(s, s1);
>         httpclient.getState().setAuthenticationPreemptive(true);
>         httpclient.getState().setCredentials(null, null,
> usernamepasswordcredentials);
>         Object obj = null;
>
>
> I want info on .Net equivalent to Java's UsernamePasswordCredentials class .
>
> This UsernamePasswordCredentials is different from .net's NetworkCredential.
> I tried below code itz not working.
>
> Please guide me on this issue.
>
> Thanks
> Santhosh
>
>
>
> public static string HttpWebServiceRequest(string url, string userName,
> string password, string xmlContent, string domain, int timeoutMS, int reqtype)
> {
>     try
>     {
>         System.Net.ServicePointManager.CertificatePolicy = new
> TrustAllCertificatePolicy();
>         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
>         request.Method = "POST";
>         request.Timeout = timeoutMS;
>         request.ContentType = "text/xml";
>         request.KeepAlive = true;
>         request.ContentLength = xmlContent.Length;
>         request.ProtocolVersion = HttpVersion.Version10;
>
>
>         if((userName != "") && (password != ""))
>         // I need to use some different class here.
>             request.Credentials = new NetworkCredential(userName, password, domain);
>             using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
>             {
>                 sw.Write(xmlContent);
>             }
>
>         using (StreamReader sr = new
> StreamReader(request.GetResponse().GetResponseStream()))
>         {
>             return sr.ReadToEnd();
>         }
>
>     }
>     catch (Exception e)
>     {
>         throw;
>     }
> }
Author
9 Jan 2007 11:13 AM
San
Hi Jakob,

ya i am using SSL.

I tried the code that you had given itz not working :(

I am in search of class which is equivalent to UsernamePasswordCredentials
java class.

Thanks
San :)

Show quote
"Jakob Christensen" wrote:

> It looks like you are using SSL?
>
> MSDN states that NetworkCredential does not work for SSL.  I have been
> unable to find out what to do instead but you might want to try using this:
>
> CredentialCache myCache = new CredentialCache();
> NetworkCredential netCredential = new 
> NetworkCredential(UserName,SecurelyStoredPassword,Domain);
> myCache.Add(new Uri(url),"Basic", netCredential);
> myCache.Add(new Uri(url),"Digest", netCredential);
> myCache.Add(new Uri(url),"Negotiate", netCredential);
> myRequest.Credentials = myCache;
>
> HTH, Jakob.
>
>
> --
> http://www.dotninjas.dk
>
>
>
Author
9 Jan 2007 12:06 PM
Jakob Christensen
Hi San,

I do not know Java and UsernamePasswordCredentials but it seems to me that
NetworkCredential is the best bet.

I found the following article explaining SSL and Basic authentication when
using web services: http://www.eggheadcafe.com/articles/20051104.asp

I know you are not trying to access a web service but to me it looks like
the same problem.  You might want to try setting
HttpWebRequest.PreAuthenticate = true as is explained in the article.

HTH, Jakob.
Show quote
"San" wrote:

> Hi Jakob,
>
> ya i am using SSL.
>
> I tried the code that you had given itz not working :(
>
> I am in search of class which is equivalent to UsernamePasswordCredentials
> java class.
>
> Thanks
> San :)
>
> "Jakob Christensen" wrote:
>
> > It looks like you are using SSL?
> >
> > MSDN states that NetworkCredential does not work for SSL.  I have been
> > unable to find out what to do instead but you might want to try using this:
> >
> > CredentialCache myCache = new CredentialCache();
> > NetworkCredential netCredential = new 
> > NetworkCredential(UserName,SecurelyStoredPassword,Domain);
> > myCache.Add(new Uri(url),"Basic", netCredential);
> > myCache.Add(new Uri(url),"Digest", netCredential);
> > myCache.Add(new Uri(url),"Negotiate", netCredential);
> > myRequest.Credentials = myCache;
> >
> > HTH, Jakob.
> >
> >
> > --
> > http://www.dotninjas.dk
> >
> >
> >
>

AddThis Social Bookmark Button