在蔚藍的虛擬機中,我有一個Web應用程序和一個帶有FormsAuthentication和HTTPS配置有效證書的子Web應用程序。主機應用程序和子應用程序使用相同的機器密鑰共享身份驗證。兩個應用程序都需要SSL遠程證書被用戶驗證爲無效
所有從外部使用公共URL都可以。
我需要從主應用程序發送一些請求到具有公共名稱的子應用程序用於配置目的(子應用程序可以安裝在另一臺服務器上)。這些請求使用特定的帳戶進行識別。
這是我的代碼從主應用程序發送請求給子應用,this.WebApiUrl是公共網址:
// 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一個解決方案,因爲它是在生產環境中
感謝您幫助
是的,我們有這個回調提前返回'如果(錯誤== SslPolicyErrors.None在同一服務器上其他Web應用程序)返回true;'。我也嘗試沒有回調,並且我的應用程序和我的子應用程序之間的請求總是有錯誤。 – Troopers
回調中的'error'的值是否表示更有用?我假設您已按照以下方式瀏覽了根證書:http://blogs.msdn.com/b/jpsanders/archive/2009/09/16/troubleshooting-asp-net-the-remote-certificate-is-invalid -according-the-validation-procedure.aspx – nullPainter
是的,我檢查了證書,證書路徑,證書頒發機構..所有沒關係。我不認爲問題是證書,因爲來自外部的請求是好的。只有主應用程序和子應用程序之間的請求失敗 – Troopers