|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
FtpWebRequest with X.509 AuthenticationI have been very confused about the purpose of ClientCertificates on
FtpWebRequest. What I need to do is authenticate to an FTP server using ONLY an X.509 certificate, no username or password. Is there any way to do this with .NET 2/3? Internet Explorer does it just fine... Hi Digineer,
For the FtpWebRequest, it will need to add client certificate if you're communicating with a FTP server that use SSL channel and require client certificate. To add client certificate, it is the same as using HttpWebRequest, in .net framework 2.0, the "System.Security.Cryptography.X509Certificates" namespace has much enhanced the functionality of processing X509 certificates(from windows certificate store or certificate file). Here is a very simple code snippet that query a certificate (through its thumbprint ) and add it into the FtpwebRequest: ============================== protected void Button1_Click(object sender, EventArgs e) { FtpWebRequest request = WebRequest.Create("ftp://ftp.mysite.com") as FtpWebRequest; request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential("username", "password"); //query certificate from store string tp = "2b6f8ac51a85cbaf429474a55304313968667611"; X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly); X509Certificate2 cert2 = store.Certificates.Find(X509FindType.FindByThumbprint, tp, true)[0]; store.Close(); //add certificate into webrequest component request.ClientCertificates.Add(cert2); FtpWebResponse response = request.GetResponse() as FtpWebResponse; Response.Write("<br/>response code: " + response.StatusCode); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); //Response.Write("<br/>response: " + sr.ReadToEnd()); TextBox1.Text = sr.ReadToEnd(); sr.Close(); response.Close(); } =============================== In addition, here are some other articles introducing working with FtpWebRequest class or the .NET framework 2.0 certificate components: #adarsh's blog : FtpWebRequest http://blogs.msdn.com/adarshk/rss.aspx?CategoryID=7225 #Support Certificates In Your Applications With The .NET Framework 2.0 http://msdn.microsoft.com/msdnmag/issues/07/03/NETSecurity/#S4 Sincerely, Steven Cheng Microsoft MSDN Online Support Lead ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. |
|||||||||||||||||||||||