远程证书被用户验证为无效
Remote certificate was verified as invalid by the user
在 azure 虚拟机中,我有一个 Web 应用程序和一个子 Web 应用程序,其中配置了有效证书的 FormsAuthentication 和 HTTPS。使用相同的机器密钥在主应用程序和子应用程序之间共享身份验证。两个应用程序需要 SSL
public url.
从外面看没问题
我需要从主应用程序发送一些请求到名称为 public 的子应用程序以进行配置(子应用程序可以安装在另一台服务器上)。这些请求使用特定帐户进行身份验证。
这是我从主应用程序向子应用程序发送请求的代码,this.WebApiUrl 是 public url:
// If we are not authenticated
if(!isAuthenticated)
{
// Check user and login
if(!User.Check(this.WebApiLogin, this.WebApiPassword))
throw new Exception("Unauthorized user");
isAuthenticated = true;
}
// Convert HttpCookie to Cookies
var cookies = FormsAuthentication.GetAuthCookie(this.WebApiLogin, false).ToCookies();
// Create request with the authentication cookie
Uri baseAddress = new Uri(this.WebApiUrl);
CookieContainer cookieContainer = new CookieContainer();
foreach(var cookie in cookies)
{
if(String.IsNullOrEmpty(cookie.Domain))
cookie.Domain = baseAddress.Host;
if(baseAddress.Scheme == "https")
cookie.HttpOnly = false;
cookieContainer.Add(cookie);
}
// send request
using(HttpClientHandler handler = new HttpClientHandler() { CookieContainer = cookieContainer })
{
using(HttpClient client = new HttpClient(handler))
{
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client.GetStringAsync(requestUri).Result;
}
}
没有 ssl 就可以了。
当我激活 ssl 时,主应用程序和子域应用程序之间的请求失败
The underlying connection was closed: Could not establish trust
relationship for the SSL/TLS secure channel..
这是来自 System.Net 和 System.Net 套接字的系统日志。
System.Net Information: 0 : [10788] SecureChannel#92992 - Remote
certificate was verified as invalid by the user.
System.Net.Sockets Verbose: 0 : [10788] Socket#29502801::Dispose()
System.Net Error: 0 : [10788] Exception in HttpWebRequest#61435094:: - The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel..
System.Net Verbose: 0 : [10788] HttpWebRequest#61435094::EndGetResponse()
System.Net Error: 0 : [10788] Exception in HttpWebRequest#61435094::EndGetResponse - The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel..
奇怪的是日志没有说明证书被用户验证为无效的原因。尽管此证书对于来自外部的请求有效。
重要:我不想要 ServicePointManager.ServerCertificateValidationCallback 的解决方案,因为它在生产环境中
谢谢你的帮助
您是否在代码的其他任何地方提供了 ServerCertificateValidationCallback
?
我们在实现的回调中存在逻辑缺陷,该回调将具有自签名证书的指定域列入白名单。即,回调始终在执行 - 即使对于有效证书 - 但 仅 应用白名单逻辑。由于合法证书不在该列表中,回调显示失败。
根据 error
变量提前返回解决了这个问题:
if (error == SslPolicyErrors.None) return true;
在 azure 虚拟机中,我有一个 Web 应用程序和一个子 Web 应用程序,其中配置了有效证书的 FormsAuthentication 和 HTTPS。使用相同的机器密钥在主应用程序和子应用程序之间共享身份验证。两个应用程序需要 SSL
public url.
从外面看没问题我需要从主应用程序发送一些请求到名称为 public 的子应用程序以进行配置(子应用程序可以安装在另一台服务器上)。这些请求使用特定帐户进行身份验证。
这是我从主应用程序向子应用程序发送请求的代码,this.WebApiUrl 是 public url:
// If we are not authenticated
if(!isAuthenticated)
{
// Check user and login
if(!User.Check(this.WebApiLogin, this.WebApiPassword))
throw new Exception("Unauthorized user");
isAuthenticated = true;
}
// Convert HttpCookie to Cookies
var cookies = FormsAuthentication.GetAuthCookie(this.WebApiLogin, false).ToCookies();
// Create request with the authentication cookie
Uri baseAddress = new Uri(this.WebApiUrl);
CookieContainer cookieContainer = new CookieContainer();
foreach(var cookie in cookies)
{
if(String.IsNullOrEmpty(cookie.Domain))
cookie.Domain = baseAddress.Host;
if(baseAddress.Scheme == "https")
cookie.HttpOnly = false;
cookieContainer.Add(cookie);
}
// send request
using(HttpClientHandler handler = new HttpClientHandler() { CookieContainer = cookieContainer })
{
using(HttpClient client = new HttpClient(handler))
{
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client.GetStringAsync(requestUri).Result;
}
}
没有 ssl 就可以了。 当我激活 ssl 时,主应用程序和子域应用程序之间的请求失败
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel..
这是来自 System.Net 和 System.Net 套接字的系统日志。
System.Net Information: 0 : [10788] SecureChannel#92992 - Remote certificate was verified as invalid by the user.
System.Net.Sockets Verbose: 0 : [10788] Socket#29502801::Dispose()
System.Net Error: 0 : [10788] Exception in HttpWebRequest#61435094:: - The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel..
System.Net Verbose: 0 : [10788] HttpWebRequest#61435094::EndGetResponse()
System.Net Error: 0 : [10788] Exception in HttpWebRequest#61435094::EndGetResponse - The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel..
奇怪的是日志没有说明证书被用户验证为无效的原因。尽管此证书对于来自外部的请求有效。
重要:我不想要 ServicePointManager.ServerCertificateValidationCallback 的解决方案,因为它在生产环境中
谢谢你的帮助
您是否在代码的其他任何地方提供了 ServerCertificateValidationCallback
?
我们在实现的回调中存在逻辑缺陷,该回调将具有自签名证书的指定域列入白名单。即,回调始终在执行 - 即使对于有效证书 - 但 仅 应用白名单逻辑。由于合法证书不在该列表中,回调显示失败。
根据 error
变量提前返回解决了这个问题:
if (error == SslPolicyErrors.None) return true;