用户已将远程证书验证为无效

Tro*_*ers 9 asp.net iis ssl azure ssl-certificate

在azure虚拟机中,我有一个Web应用程序和一个子Web应用程序,其中FormsAuthentication和HTTPS配置了有效的证书.使用相同的机器密钥在主应用程序和子应用程序之间共享身份验证.两个应用程序需要SSL

公共网址从外面都可以.

我需要从主应用程序向子应用程序发送一些请求,并使用公共名称进行配置(子应用程序可以安装在另一台服务器上).这些请求使用特定帐户进行识别.

这是我从主应用程序向子应用程序发送请求的代码,this.WebApiUrl是公共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;
    }
}
Run Code Online (Sandbox Code Playgroud)

没有ssl就没关系.当我激活ssl时,主应用程序和子域应用程序之间的请求失败

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

这是System.Net和System.Net套接字的系统日志.

System.Net信息:0:[10788] SecureChannel#92992 - 用户验证远程证书无效.

System.Net.Sockets详细:0:[10788]套接字#29502801 :: Dispose()

System.Net错误:0:[10788] HttpWebRequest中的异常#61435094 :: - 底层连接已关闭:无法为SSL/TLS安全通道建立信任关系.

System.Net详细:0:[10788] HttpWebRequest#61435094 :: EndGetResponse()

System.Net错误:0:[10788] HttpWebRequest中的异常#61435094 :: EndGetResponse - 底层连接已关闭:无法为SSL/TLS安全通道建立信任关系.

奇怪的是,日志没有说明为什么证书被用户验证为无效.此证书对外部请求有效.

重要提示:我不想要ServicePointManager.ServerCertificateValidationCallback的解决方案,因为它位于生产环境中

谢谢你的帮助

nul*_*ter 2

您是否ServerCertificateValidationCallback在代码中的其他位置提供了 a ?

我们实现的回调中存在逻辑缺陷,该回调将具有自签名证书的指定域列入白名单。也就是说,回调始终在执行 - 即使对于有效证书 - 但应用白名单逻辑。由于合法证书不在此列表中,因此回调指示失败。

这是通过根据error变量提前返回来解决的:

if (error == SslPolicyErrors.None) return true;