![](/img/trans.png)
[英]Is there a way to use FtpWebRequest to authenticate to FTP using client certificates in C#?
[英]Is there a way to use FtpWebRequest to authenticate to FTP using client certificates in C# which can work from iis also?
我嘗試了stackoverflow給出的解決方案。
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com");
request.Credentials = new NetworkCredential("username", "");
// Query certificate from store
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
const string tp = "2b6f8ac51a85cbaf429474a55304313968667611";
X509Certificate2 cert2 = store.Certificates.Find(X509FindType.FindByThumbprint, tp, true)[0];
store.Close();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com");
request.Credentials = new NetworkCredential("username", "");
// Query certificate from store
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
const string tp = "2b6f8ac51a85cbaf429474a55304313968667611";
X509Certificate2 cert2 = store.Certificates.Find(X509FindType.FindByThumbprint, tp, true)[0];
store.Close();
// Add certificate into request
request.ClientCertificates.Add(cert2);
從控制台應用程序運行時,這非常理想。 但是我已經在iis中部署了它,並在發生錯誤后從iis運行。
An exception of type 'System.Security.Authentication.AuthenticationException' occurred in System.dll but was not handled in user code
Additional information: A call to SSPI failed, see inner exception.
Inner exception:
{"An unknown error occurred while processing the certificate"}
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.Stream.Dispose()
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.GetResponse()
at Demo.Controllers.HomeController.GetAndDownloadFTPFile()
at Demo.Controllers.HomeController.Index()
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
有人可以指導我嗎?
我不確定其他所有內容,但是您的IIS應用程序池可能無法訪問您認為在StoreLocation.CurrentUser
的證書
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
即我的假設是它找不到證書
我的問題已解決,請使用受信任的證書文件夾安裝p12證書。 雙擊p12認證並與本地用戶一起安裝它,最后瀏覽文件夾並添加到受信任的文件夾中。 這個對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.