繁体   English   中英

Visual Studio 2012 + 403.7错误

[英]Visual Studio 2012 + 403.7 Error

我已经转动了几天,现在没有任何进展,到目前为止,我在网上发现的建议还没有完全解决问题,因此,这里是:

  1. 我有一个与WCF服务相关联的...不确定在另一端您将调用哪种类型的Web服务,即REST风格。 该方法的URL如下所示:“ https://partner.someservice.com/SomeMethod.asp ”。 我将一些查询字符串args塞到其末尾,然后将请求发布到他们的服务器。

  2. VS中的错误仅显示403,但是当我使用Fiddler时看到403.7。 在将证书导入浏览器之前,我还看到了403.7。 我可以检查我的请求对象,并查看指定了[1]证书的ClientCertificates,因此,我很确定它已附加。

  3. 我已将.pfx文件导入到我的计算机和本地用户证书存储中。 我已经以多种方式多次运行winhttpcertcfg实用程序,基本上遵循了我在MSDN和SO帖子上看到的说明。

    winhttpcertcfg -g -c LOCAL_MACHINE \\ MY -s [cert] -a [用户/ aspnet /授权用户]

    winhttpcertcfg.exe -i [cert.pfx] -c LOCAL_MACHINE \\ MY -p [pwd]

  4. 我已将.pfx文件导入Chrome。 如果我在Chrome中点击该URL,则会提示您选择我的证书,然后可以继续使用该URL。 在IE中的行为相同。

因此,这似乎是针对我在VS中运行的,尽管我不确定我没有检查过哪个选项或未授予什么权限。 在WCF服务上的项目属性下,我尝试了“使用Visual Studio开发服务器”和“使用本地IIS Web服务器”,但是两者的行为相同。 我想念什么?

一些代码:

    private static string DoPost(string postData)
    {
        string result = null;

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(GetEndpoint());
        var encoding = new ASCIIEncoding();
        var bytes = encoding.GetBytes(postData);

        httpWebRequest.Method = "POST";
        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
        httpWebRequest.ContentLength = bytes.Length;
        httpWebRequest.ClientCertificates.Add(clientCertificate);

        using (var stream = httpWebRequest.GetRequestStream())
        {
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();

            using (var httpWebResponse = httpWebRequest.GetResponse())
            using (var responseStream = httpWebResponse.GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (var reader = new StreamReader(responseStream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
        }

        return result;
    }

和获得证书的代码(在另一个开发人员编写的实用程序类中,这似乎在生产中可以正常工作):

    public static X509Certificate2 GetCertificateBySerial(string serial)
    {
        var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

        var certColl = store.Certificates.Find(X509FindType.FindBySerialNumber, serial, false);
        store.Close();

        if (certColl.Count == 0)
        {
            throw new Exception(String.Format("A certificate with a serial number of \"{0}\" is not installed on the server.", serial));
        }
        return certColl[0];
    }

我进行了一些更改,并使其能够正常工作。 首先,我不得不从GET更改为POST *,这样做时我不小心将部分端点/ URL留在了post数据中(哎呀)。 其次,我将其包含在我的httpWebRequest中:

httpWebRequest.ProtocolVersion = HttpVersion.Version11;

最后,403.7错误实际上是由Fiddler拦截我的流量引起的。 我没有在Fiddler中正确配置此证书,但是接下来会出现。 一旦我关闭Fiddler并正确格式化了我的POST数据,事情就解决了。

*从GET切换到POST与解决此问题无关,只是一些背景故事。

TL; DR我的请求格式不正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM