标签: webexception

WebException无法为SSL/TLS安全通道建立信任关系

我的公司开发了一个.NET Web服务和一个使用该Web服务的客户端DLL.Web服务通过ssl托管在我们的服务器上,证书由GoDaddy提供并签名.我们在托管环境中有一些客户端在尝试访问我们的Web服务时从客户端DLL收到以下错误消息.

System.Net.WebException底层连接已关闭:无法为SSL/TLS安全通道建立信任关系.

我们的修复方法是让它们在服务器上打开IE,这对很多托管服务本身就是一个挑战,并转到WSDL网址.IE然后通过安全警报对话框提示他们.它表示证书日期有效且名称与页面名称相匹配,但由您未选择信任的公司发布.当他们单击是继续时,客户端DLL可以成功连接到Web服务并正常运行.

有谁知道为什么GoDaddy不会出现在有效的出版商名单中?我们运行的所有服务器都将GoDaddy作为有效权限.我猜测,出于安全原因,他们已经卸载了GoDaddy的权限,但并不完全相信没有其他潜在的问题.

不幸的是,我没有太多运气试图在本地重新创建.如果我进入Internet选项并删除GoDaddy权限并点击我们的服务,ssl工作正常.我回到发布者列表中,GoDaddy重新进入.所以我的第二个问题是,你如何摆脱GoDaddy所以我可以获得无效的证书警告?

好的,最后一个问题.代码中是否有一种方法可以告诉Web服务忽略无效的证书.我已经看到一些帖子以编程方式使用WCF而不是旧的Web服务.

c# asp.net ssl web-services webexception

5
推荐指数
2
解决办法
5万
查看次数

HttpWebRequest导致"请求被中止:无法创建SSL/TLS安全通道"

我们在Classic ASP应用程序中引入了一些.NET页面.当用户点击其中一个.NET页面时,我们使用HttpWebRequest通过将请求发送到具有我们想要的会话变​​量名称的类ASP页面来从ASP端获取会话变量值.除以下情况外,此方法有效.

在我们的生产服务器上,我们的IT人员通过一些注册表黑客攻击禁用了弱密码算法.我们想要强制128超过128(或者我告诉那些比我更了解安全性的人).但是,这会导致我们的会话共享请求中止并出现以下错误.

The request was aborted: Could not create SSL/TLS secure channel
Run Code Online (Sandbox Code Playgroud)

我们打开了跟踪日志记录,如本文所述.相关的线条似乎是这些.

System.Net Information: 0 : [3892] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 1b6acdd0:171a28, targetName = secure.xxx.com, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [3892] InitializeSecurityContext(In-Buffers count=2, Out-Buffer length=0, returned code=AlgorithmMismatch).
System.Net.Sockets Verbose: 0 : [3892] Socket#18136189::Dispose()
System.Net Error: 0 : [3892] Exception in the HttpWebRequest#51004322:: - The request was aborted: Could not create SSL/TLS secure channel.
System.Net Error: 0 : [3892] Exception …
Run Code Online (Sandbox Code Playgroud)

ssl httpwebrequest webexception

5
推荐指数
1
解决办法
6903
查看次数

问题SSL证书C#

在我的C#应用​​程序中,我必须通过https调用Web服务并使用我已经拥有的.crt文件进行验证.以下是满足此类需求的正确解决方案.我得到一个有效的解决方案后,我已经更新了这篇文章,认为它可能会帮助像我这样的人.

解决方案: 以下代码只需在整个应用程序执行中执行一次.有了这个,我们设置ServerCertification和SSL属性,每当调用reqest时将使用它们:

        public static void setSSLCertificate()
    {
        clientCert = new X509Certificate2(AUTHEN_CERT_FILE); // Pointing to the .crt file that will be used for server certificate verification by the client
        System.Net.ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateValidation);
    }

    public static bool customXertificateValidation(Object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPoicyErrors)
    {
        switch (sslPoicyErrors)
        {
            case System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors:
            case System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch:
            case System.Net.Security.SslPolicyErrors.RemoteCertificateNotAvailable:
                break;
        }

        return clientCert.Verify();  // Perform the Verification and sends the result
    }
Run Code Online (Sandbox Code Playgroud)

请求通常像我们没有实现SSL那样完成.这是一个Post请求代码:

        private static String SendPost(String uri, String post_data)
    {
        String resData = …
Run Code Online (Sandbox Code Playgroud)

c# exception ssl-certificate webexception

5
推荐指数
1
解决办法
2万
查看次数

C# 如何使用响应状态代码创建 WebException

我做了一些假类,它应该与原始类具有相同的行为。原始类有时会抛出 WebExceptions(StatusCode来自服务器的响应)。

我想在没有任何联系的情况下重复这种行为。那么,我如何创建new WebException(..., ..., ..., ...)所需的StatusCode

c# exception http-status-codes webexception

5
推荐指数
1
解决办法
6577
查看次数

如何解决.NET.CORE中的代理服务器407错误

所以我有一些代码可以访问网站的 html,但只能在 Visualstudios.Framework for c# 中将此代码输入到 app.config 中。

    <system.net> <defaultProxy useDefaultCredentials="true" /> </system.net>
Run Code Online (Sandbox Code Playgroud)

ps >< 之间的下一行

但我需要此代码在 .CORE 而不是 .FRAMEWORK 中工作,但它给出了此错误。

System.Net.WebException:“远程服务器返回错误:(407)需要代理身份验证。”

我尝试通过从解决方案资源管理器创建 app.config 来解决它,但它是否有效?互联网上的所有答案要么太复杂,要么显示我在 .FRAMEWORK 的 app.config 中放入了一点代码这不适用于 .Core

代码作为起点。

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = null;
            if (response.CharacterSet == null)
            {
                readStream = new StreamReader(receiveStream);
                Console.WriteLine("Sorry , somethings faulty");

            }
            else
            {
                readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                readStream.ToString();
                // Console.WriteLine(readStream);
            }
            string ImpureTexta = …
Run Code Online (Sandbox Code Playgroud)

c# system.net.webexception webexception .net-core

3
推荐指数
1
解决办法
9442
查看次数

状态代码301在C#中无法正确显示

我可以从HttpWebRequest和HttpWebResponse获取Http状态代码( 200,301,404 等)中的dtb建议获得枚举数字.但是,对于移动永久站点,我也得到200(OK).我想看到的是301.请帮忙.我的代码如下.可能有什么不对/需要纠正?

 public int GetHeaders(string url)
{
    //HttpStatusCode result = default(HttpStatusCode);
    int result = 0;
    var request = HttpWebRequest.Create(url);
    request.Method = "HEAD";
    try
    {
        using (var response = request.GetResponse() as HttpWebResponse)
        {
            if (response != null)
            {
                result = (int)response.StatusCode; // response.StatusCode;
                response.Close();
            }
        }
    }
    catch (WebException we)
    {
        if (we.Response != null)
        {
            result = (int)((HttpWebResponse)we.Response).StatusCode;
        }

    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

我使用此代码的工具能够显示404,而不是现有域,但它忽略重定向并显示有关重定向URL的详细信息.例如,如果我将旧域easytipsandtricks.com放在文本字段中,它会显示tipscow.com的结果(如果您在任何在线检查器工具中检查easytipsandtricks.com,您会注意到它提供了正确的重定向消息 - 301 Moved ).请帮忙.

c# webexception

1
推荐指数
1
解决办法
1604
查看次数

无法捕获从任务抛出的异常

我对此已经摸不着头脑有一段时间了。我不知道这个异常是在哪里抛出的。我到底应该在哪里捕获这个异常?它是System.Net.WebException。我想我只是没有得到任务和异步/等待。我知道我正在创建其他线程,这很好,但是我在哪里处理异常?

The remote server returned an error: (403) Forbidden.

这是代码:

public async void sendRequest(string database, string table, string where, bool isPost, int deviceType)
{
    string urlQuery = Constants.URL_QUERY + database + "/" + table + "?" + where;

    Debug.WriteLine("URL Query: " + urlQuery);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlQuery);
    request.Method = isPost ? "POST" : "GET";
    request.ContentType = "application/json";

    try
    {
        Debug.WriteLine("getting response");
        WebResponse response = await makeAsyncRequest(request);
        Debug.WriteLine("response obtained");
        finishRequest(response);
    }
    catch (WebException e)
    {
        Debug.WriteLine("WebException caught");
    }
} …
Run Code Online (Sandbox Code Playgroud)

c# task webexception async-await

0
推荐指数
1
解决办法
1516
查看次数