相关疑难解决方法(0)

WebRequest错误 - 无法创建SSL/TLS安全通道

我正在尝试编写C#代码,该代码针对用于计算Web应用程序中的销售税的REST服务端点发出Web请求.这是第三方服务,使用SSL进行保护.有两种环境,UAT和生产.运行webrequest的代码如下所示:

...

var req = WebRequest.Create(url) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/json";

...

using (var webresponse = req.GetResponse())
{
    using (var responseStream = new StreamReader(webresponse.GetResponseStream()))
    {
        var respJson = responseStream.ReadToEnd();
        calcResult = BuildResponse(calcRequest, respJson, consoleWriteRawReqResponse);
    }
}

return calcResult;
Run Code Online (Sandbox Code Playgroud)

这对UAT环境很好.但是当我在生产环境中运行相同的代码时,我收到错误:

"无法创建SSL/TLS安全通道"

能没有问题,从邮差执行这两个请求,没有任何特殊的修改.

这导致我走上了调查这个错误的道路,我找到了很多有用的SO帖子来讨论这个话题,包括:

请求已中止:无法创建SSL/TLS安全通道

尽管设置了ServerCertificateValidationCallback,但无法创建SSL/TLS安全通道

这些帮助指向了我正确的方向,即查看将ServicePointManager.SecurityProtocol设置设置为不同的值,并使用ServicePointManager.ServerCertificateValidationCallback来调查错误.

我玩这些后发现的是以下内容:

  • UAT环境调用将使用Ssl3 |的默认设置 Tls(.NET 4.5.2的默认值),而生产环境则不会.
  • 只有当我将此设置显式设置为Ssl3时,生产调用才会起作用.

该代码如下所示:

...

var req = WebRequest.Create(url) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/json";

...

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback …
Run Code Online (Sandbox Code Playgroud)

c# ssl https

8
推荐指数
2
解决办法
3295
查看次数

How to implement "SecurityProtocolType.Ssl, Ssl1, Ssl2, Ssl3; " through web.config not in global.asax.cs

My question is simple, I have read these two main pages :

But from the first link, it's showing configuration for SecurityProtocol set in global.asax.cs for solving

"System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel."

Here, I want this config is set in web.config / app.config, just for making it a little specific for own project not for all asp.net projects... Then I …

asp.net ssl wcf web-config tls1.2

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

KB4344167安全更新中断了TLS代码

希望有人可以帮助解决这个问题.最近我们的机器更新了KB4344167,其中包括.NET 4.7.1的安全更新. 不幸的是,这个更新破坏了我们的代码Webrequest.当我们运行下面的代码时,我们收到此错误:

请求已中止:无法创建SSL/TLS安全通道.

// Create a request for the URL.        
WebRequest request = WebRequest.Create(url);
//specify to use TLS 1.2 as default connection
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
request.Timeout = int.Parse(configmanager.GetSetting("Webtimeout"));
// Set proxy
request.Proxy = WebRequest.DefaultWebProxy;
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
// Define a cache policy for this request only. 
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
// Get the response.
HttpWebResponse response …
Run Code Online (Sandbox Code Playgroud)

c# .net-4.7

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

IDX10803:无法创建从以下位置获取配置:“https://login.microsoftonline.com/{Tenant-ID}/.well-known/openid-configuration”

在过去的 5 年里,我一直在使用 Azure IoT 远程监控解决方案并使用 Azure AD 身份验证来保护应用程序和 API,从上周六开始,我在登录时收到以下错误(黄色屏幕):

IDX10803:无法创建从“https://login.microsoftonline.com/{Tenant-ID}/.well-known/openid-configuration”获取配置。

这是我的身份验证相关的启动代码:

public void ConfigureAuth(IAppBuilder app, IConfigurationProvider configProvider)
    {
        string aadClientId = configProvider.GetConfigurationSettingValue("ida.AADClientId");
        string aadInstance = configProvider.GetConfigurationSettingValue("ida.AADInstance");
        string aadTenant = configProvider.GetConfigurationSettingValue("ida.AADTenant");
        string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, aadTenant);

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());


        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
            new WindowsAzureActiveDirectoryBearerAuthenticationOptions
            {
                Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                TokenValidationParameters = new TokenValidationParameters { SaveSigninToken = true, ValidAudience = ConfigurationManager.AppSettings["ida:Audience"] }
            });


        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = aadClientId,
                Authority = authority,
                TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = false
                },
                Notifications …
Run Code Online (Sandbox Code Playgroud)

azure azure-active-directory openid-connect

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

绕过证书验证

我无法绕过生产服务器中的证书验证,因为ServicePointManager.ServerCertificateValidationCallback从未调用过:

ServicePointManager.ServerCertificateValidationCallback = ValidateRemoteCertificate;

...

private bool ValidateRemoteCertificate(
    object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors)
{
    LogActionToTable(EntrySeverity.Information, 
        $"ValidateRemoteCertificate() Certificate name: {cert.Subject}");
    return true;
}
Run Code Online (Sandbox Code Playgroud)

当从 Web 应用程序中的方法调用时,该行永远不会执行,奇怪的是它是从单元测试 (NUnit) 中正确调用的。

我确实在对远程 API 进行任何其他调用之前添加了此调用。

我究竟做错了什么?

如果我这样做,它甚至不起作用:

ServicePointManager
  .ServerCertificateValidationCallback += 
    (sender, cert, chain, sslPolicyErrors) => true;
Run Code Online (Sandbox Code Playgroud)

这一定非常简单,但我已经研究了好几天了,但我无法让它正常工作。


无论我是使用手动构建 XML 的 RestSharp 调用,还是通过wsdl.exe工具生成的代理进行调用,我总是会从调用中获取以下内容:

请求被中止:无法创建 SSL/TLS 安全通道。

并启用跟踪到日志文件中,我得到:

System.Net Information: 0 : [7912] SecureChannel#14469269 - Certificate is of type X509Certificate2 and contains the private key. System.Net Information: …

c# ssl

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

TLS 1.2 - 提供给函数的令牌无效

我有这个奇怪的问题,SslStream.AuthenticateAsClient() 抛出以下异常:

System.Security.Authentication.AuthenticationException : A call to SSPI failed, see inner exception.
  ----> System.ComponentModel.Win32Exception : The token supplied to the function is invalid
Run Code Online (Sandbox Code Playgroud)

仅当客户端需要 Tls12 时才会发生:

SslStream.AuthenticateAsClient(..., ..., SslProtocols.Tls12);
Run Code Online (Sandbox Code Playgroud)

代码适用于 Ssl3、Tls11 和 Tls。

服务器证书是自签名的并且是“旧的”。它使用 md5RSA 签名和 1024 位,最初我认为这就是问题所在,因为重新生成证书会使异常消失(我厌倦了 SHA1 和 SHA512 - 都可以)。

然而,令我惊讶的是,FileZilla 客户端能够使用这个“旧”md5RSA 签名证书并使用TLS 1.2连接到该服务器:

使用旧证书的 TLS 1.2

这是证书:

-----BEGIN CERTIFICATE-----
MIICejCCAeOgAwIBAgIQzbvZdHHAV49D7R8OE2mEaDANBgkqhkiG9w0BAQQFADBA
MSswKQYJKoZIhvcNAQkBFhxlbWFpbEBpbi10aGUtY2VydGlmaWNhdGUuY29tMREw
DwYDVQQDEwhKb2huIERvZTAeFw0xMTAxMjAyMDAzNDFaFw0zOTEyMzEyMzU5NTla
MEAxKzApBgkqhkiG9w0BCQEWHGVtYWlsQGluLXRoZS1jZXJ0aWZpY2F0ZS5jb20x
ETAPBgNVBAMTCEpvaG4gRG9lMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0
s5RAKYdw2AYk3t0oH5jDo6RQRRfabkOLfKvR8kOiYbqjtgblx7JhSZJHX/r6KLoc
hGgYkQPOSKnl8TdgEkzPxWHECV/iMdOxTsTv2P//ZM2INjb4H8JjDS16PYFwHP3w
/9RU6PjppK+mPdWP1pezBzebSM0QQwpmXlSmfe2ULQIDAQABo3UwczBxBgNVHQEE
ajBogBA4WNgTvhkmRD8DhHeRvAJcoUIwQDErMCkGCSqGSIb3DQEJARYcZW1haWxA
aW4tdGhlLWNlcnRpZmljYXRlLmNvbTERMA8GA1UEAxMISm9obiBEb2WCEM272XRx
wFePQ+0fDhNphGgwDQYJKoZIhvcNAQEEBQADgYEAFX6MM/E97hC6t1TAFBmM3tWr
fQ2cB0LFCe6J0I8phKQecpSYCkMdvaHdsT+sdzXNW4bgL064r731r8l/47VgfgIR
oRmsQYnwJ55nqZpEW2zL3vioedWiCVto8X9/dVC8jqPpcmMP5NWBHh88o7nkPBxe
C8iucrQvHnjYwaz1o/M=
-----END CERTIFICATE-----
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. 为什么SslStream.AuthenticateAsClient会抛出异常,而 Filezilla 客户端却能够连接?

  2. 使用 TLS 1.2 时是否有任何 …

.net ssl cryptography cryptoapi tls1.2

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

无法创建 SSL/TLS 安全通道 - GetRequestStream()

我正在尝试将 paypal 集成到 Web 应用程序中,但没有成功。我已经尝试了很多东西,但我总是回到一个特定的错误。我现在正在尝试使用 paypal 集成向导,当我获得提供的代码时,我收到一条错误消息:请求已中止:无法创建 SSL/TLS 安全通道。

这是代码:

public string HttpCall(string NvpRequest) //CallNvpServer
{
    string url = pendpointurl;

    //To Add the credentials from the profile
    string strPost = NvpRequest + "&" + buildCredentialsNVPString();
    strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode( BNCode );

    HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
    objRequest.Timeout = Timeout;
    objRequest.Method = "POST";
    objRequest.ContentLength = strPost.Length;
    StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream());
Run Code Online (Sandbox Code Playgroud)

错误发生在最后一行,即 objRequest.GetRequestStream()

我尝试在 google 上查找它,但没有找到任何对我有用的东西。有谁知道我能做些什么来解决这个问题?

asp.net ssl paypal

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