FtpWebRequest with X.509 Authentication

G

Guest

I 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...
 
S

Steven Cheng[MSFT]

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.
 
S

Steven Cheng[MSFT]

Hi Digineer,

How are you doing on this, does the information in my last reply helps you
some? If there is still anything we can help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Joined
Feb 23, 2015
Messages
1
Reaction score
0
Hi,

I am using the same kind of code in my .Net application to connect to secure FTP.
We've client certificates attached in mmc.

At the end, I am getting below error message.

.
.
.
System.Net Information: 0 : [3724] AcquireCredentialsHandle(package = Microsoft Unified Security

Protocol Provider, intent = Outbound, scc = System.Net.SecureCredential)
System.Net Error: 0 : [3724] AcquireCredentialsHandle() failed with error 0X8009030D.
System.Net Information: 0 : [3724] AcquireCredentialsHandle(package = Microsoft Unified Security

Protocol Provider, intent = Outbound, scc = System.Net.SecureCredential)
System.Net Error: 0 : [3724] AcquireCredentialsHandle() failed with error 0X8009030D.
System.Net.Sockets Verbose: 0 : [3724] Socket#57580907::Dispose()
System.Net Information: 0 : [3724] FtpWebRequest#38876184::(Releasing FTP connection#25929087.)
System.Net Error: 0 : [3724] Exception in FtpWebRequest#38876184::GetRequestStream - The remote server

returned an error: 234 SecurFTP: SSL starting
..
at System.Net.FtpWebRequest.CheckError()
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.IO.Stream.Close()
at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32

creationTimeout, Boolean canReuse)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetRequestStream()
System.Net Verbose: 0 : [3724] Exiting FtpWebRequest#38876184::GetRequestStream()


Kindly help me
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top