【问题标题】:HttpWebRequest, TLS and application pool service identityHttpWebRequest、TLS 和应用程序池服务标识
【发布时间】:2020-01-26 23:14:17
【问题描述】:

我们使用HttpWebRequest 对远程服务器进行网络服务调用。该调用需要从本地计算机加载证书。

我们遇到的问题是,如果应用程序池标识设置为NetworkService,那么调用将失败,HttpWebResponse.GetResponse() 调用上出现一般 TLS/SSL 错误。如果我们将应用池用户的身份更改为LocalSystem,则它可以正常运行。

我想弄清楚的是为什么LocalSystem 可以访问哪些NetworkService 不会影响 post call?

代码很简单:

    string URL = "https://mywebserver.net";
    X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
    store.Open(OpenFlags.ReadOnly);

    var certResults = store.Certificates.Find(X509FindType.FindBySubjectName, "nameofthecertificate", false);

    /* I have confirmed that the certificate is found regardless of 
     * how the application pool is configured.
    */
    X509Certificate cert = certResults[0];

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);

    req.AllowAutoRedirect = true;

    req.ClientCertificates.Add(cert);

    req.Method = "POST";

    req.ContentType = "application/soap+xml;charset=UTF-8";

    StringBuilder postData = new StringBuilder();
    postData.Append("some soap info");

    byte[] postBytes = Encoding.UTF8.GetBytes(postData.ToString());

    using (Stream postStream = req.GetRequestStream())
    {
        postStream.Write(postBytes, 0, postBytes.Length);
        postStream.Flush();
        postStream.Close();
    }

    // dies here when running under NetworkService
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    String data = String.Empty;

    using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
    {
        data = reader.ReadToEnd();
        reader.Close();
    }

【问题讨论】:

  • App pool Identity 是否有权访问该证书?
  • @Jimenemex:certificates.Find 调用确实找到了证书。我还有什么需要检查的吗?

标签: c# httpwebrequest applicationpoolidentity


【解决方案1】:

一般来说,应用程序池需要一个身份才能运行。您可以配置身份使其像您一样运行,但请记住,不同的身份具有不同级别的权限。

您的问题可能与应用程序池标识没有权限一样简单 使用它试图执行的代码。您可以通过查看已部署的项目文件夹权限来查看。

由于您提到 SSL/TLS 错误,我会认为该身份对已安装的证书没有权限。打开mmc 并检查Local Computer/Personal/Certificates 存储中是否有证书。如果这样做,请右键单击证书并转到All Tasks -> Manage Private Keys...。通过从您的计算机位置添加应用程序池标识来添加它:

IIS AppPool\YourNameOfAppPool.

你不应该得到另一个对话框说它没有找到你正确添加它。

详细了解应用程序池标识here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2013-04-09
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多