asp.net - 远程证书被用户验证为无效

标签 asp.net azure iis ssl ssl-certificate

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

从外部使用公共(public)网址就可以了。

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

这是我从主应用程序向子应用程序发送请求的代码,this.WebApiUrl 是公共(public)网址:

// 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) 返回 true;

关于asp.net - 远程证书被用户验证为无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32720197/

相关文章:

c# - PDFsharp - 使用私有(private)字体

azure - 应用程序见解 - user_AuthenticatedId 到 Azure UPN

azure - 本地 helm 存储库未从 Azure 容器注册表更新

Azure数据工厂slicestart格式错误

asp.net - 为每个网站/应用程序创建单独的IIS应用程序池的缺点

iis - SystemWeb Host : IAppBuilder. UseWebApi 忽略带有扩展名的路径

asp.net - 自定义 asp.net 成员(member)资格提供程序

c# - 运行 JavaScript setInterval 的网站在 ~1 天后开始失败

c# - trace元素中的autoflush属性有什么作用

.net - 在 IIS 上部署 .NET MVC 4 应用程序时,物理路径应指向何处