有没有一种方法可以使用C#中的客户端证书使用FtpWebRequest对FTP进行身份验证,而C#也可以从iis进行身份验证?

有没有一种方法可以使用C#中的客户端证书使用FtpWebRequest对FTP进行身份验证,而C#也可以从iis进行身份验证?,c#,C#,我尝试了stackoverflow给出的解决方案 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com"); request.Credentials = new NetworkCredential("username", ""); // Query certificate from store X509Store store = new X509Store(StoreNa

我尝试了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()
System.dll中发生“System.Security.Authentication.AuthenticationException”类型的异常,但未在用户代码中处理
其他信息:调用SSPI失败,请参阅内部异常。
内部异常:
{“处理证书时发生未知错误”}
在System.Net.FtpWebRequest.SyncRequestCallback中(对象obj)
在System.Net.FtpWebRequest.RequestCallback(对象obj)中
位于System.Net.CommandStream.Dispose(布尔处理)
在System.IO.Stream.Close()处
在System.IO.Stream.Dispose()处
位于System.Net.ConnectionPool.Destroy(PooledStream PooledStream)
在System.Net.ConnectionPool.PutConnection(PooledStream PooledStream、对象所有者Object、Int32 creationTimeout、布尔值canReuse)
在System.Net.FtpWebRequest.FinishRequestStage(请求阶段)中
在System.Net.FtpWebRequest.GetResponse()中
在Demo.Controllers.HomeController.GetAndDownloadFTPFile()中
在Demo.Controllers.HomeController.Index()中
在lambda_方法中(闭包、控制器基、对象[])
位于System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[]参数)
位于System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)
在System.Web.Mvc.Async.AsyncControllerActionInvoker.b_u39(IAsyncResult asyncResult,ActionInvokeInnerInvokeState)
位于System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
位于System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResultBase`1.End()
位于System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()中
在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.c__DisplayClass46.b__3f()中

有人能为我指点迷津吗?

我不确定其他的一切,但是您的IIS应用程序池可能无法访问您认为位于
存储位置的证书。CurrentUser

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

也就是说,我的假设是它找不到证书

我的问题已解决,请使用受信任的证书文件夹安装p12证书。双击p12认证并与本地用户一起安装,最后浏览文件夹并添加到受信任的文件夹中。它对我有效。

我已尝试从特定位置从证书加载。它在哪一行失败