Eddie,

Can you clarify - are you using a server certificate (standard https) or
are you using a client certificate? The code below appears as if you're
using a client certificate.

I retrieve files over HTTPS with just a server certificate like this:


public static bool Retrieve(string url, string userName, string
password, string fileName)
{
        WebClient client = new WebClient();
        client.Credentials = new NetworkCredential(userName, password);
        ServicePointManager.ServerCertificateValidationCallback =
ValidateServerCertificate;

        try
        {
                client.DownloadFile(url, fileName);
                return true;
        }
        catch
        {
                return false;
        }
}

public static bool ValidateServerCertificate(
          object sender,
          X509Certificate certificate,
          X509Chain chain,
          System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
        if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
                return true;

        Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

        // Do not allow this client to communicate with unauthenticated
servers.
        return false;
}


In my case, I have to log into the remote server as well, hence the
username, password and the NetworkCredential - you can remove those if
the remote server is not protected.

Cheers,
Dino

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Eddie Lascu
Sent: Wednesday, 11 July 2007 02:25
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Accessing files over HTTPS -> for Peter
Ritchie

Hello Peter and everybody,

I did some progress on this problem, but still have some issues. This is
what I did so far:


// obtain a collection of certificates
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); collection
= (X509Certificate2Collection)store.Certificates;
fcollection =
(X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid
,
DateTime.Now, false);

// URL for my file to download
string myFile2Get = "https://www.MySecureWebsite.com/MyFile.txt";;

// create the specialized Web Request object HttpWebRequest
objHttpWebReq = WebRequest.Create(myFile2Get) as HttpWebRequest;

// add the collection of the certificates
objHttpWebReq.ClientCertificates = fcollection;

// default method is GET

// get the response to my request
HttpWebResponse response = objHttpWebReq.GetResponse() as
HttpWebResponse;

// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream(); Encoding encode =
System.Text.Encoding.GetEncoding("utf-8");

// Pipes the stream to a higher level stream reader with the required
encoding format.
StreamReader readStream = new StreamReader(receiveStream, encode);

Console.WriteLine("\r\nResponse stream received.");

Char[] read = new Char[256];

// Reads 256 characters at a time.
int count = readStream.Read(read, 0, 256);

Console.WriteLine("HTML...\r\n");

while (count > 0)
{
   // Dumps the 256 characters on a string and displays the string to
the console.
   String str = new String(read, 0, count);
   Console.Write(str);
   count = readStream.Read(read, 0, 256); }

Console.WriteLine("");

// Releases the resources of the response.
response.Close();

// Releases the resources of the Stream.
readStream.Close();


There is no error thrown, but instead of getting the content of the text
file I am trying to read, all I get is message saying "Virtual user
'NameOfMyCertificate' is logged in". What am I doing wrong? How do I get
to read the file and not just some status message? Is this something
that is done in two steps: connect to the secure site and then request
the file? If so, what would be the sequence?

Any help will be highly appreciated.

All the best,
Eddie



-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of Peter Ritchie
Sent: Monday, July 09, 2007 1:24 PM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Accessing files over HTTPS -> for Peter
Ritchie


Hi Eddie, I haven't done it; but (if you want to use WebClient) I
believe you'll have to specialize WebClient and override the
GetWebRequest method.  If you've got an http[s] URL, the default should
create an HttpWebRequest object (you'll have to cast from the WebRequest
return from the base, i.e. call the base first), and HttpWebRequest has
a ClientCertificates collection property.  You can add a
X509Certificate/X509Certificate2 object to this collection.
X509Certificate has lots of ways to load from file; or you can enumerate
those stored in a store via the X509Store class[1].

Let us know how that works out.

[1] http://msdn2.microsoft.com/en-
us/library/system.security.cryptography.x509certificates.x509certificate
2co
llection(VS.80).aspx

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to