/// <summary> /// Returns the first header or throws if the header isn't found. /// </summary> public static uint GetResponseHeaderNumberInfo(SafeWinHttpHandle requestHandle, uint infoLevel) { uint result = 0; uint resultSize = sizeof(uint); if (!Interop.WinHttp.WinHttpQueryHeaders( requestHandle, infoLevel | Interop.WinHttp.WINHTTP_QUERY_FLAG_NUMBER, Interop.WinHttp.WINHTTP_HEADER_NAME_BY_INDEX, ref result, ref resultSize, IntPtr.Zero)) { WinHttpException.ThrowExceptionUsingLastError(); } return(result); }
public void ChangeDefaultCredentialsPolicy( SafeWinHttpHandle requestHandle, uint authTarget, bool allowDefaultCredentials) { Debug.Assert(authTarget == Interop.WinHttp.WINHTTP_AUTH_TARGET_PROXY || authTarget == Interop.WinHttp.WINHTTP_AUTH_TARGET_SERVER); uint optionData = allowDefaultCredentials ? (authTarget == Interop.WinHttp.WINHTTP_AUTH_TARGET_PROXY ? Interop.WinHttp.WINHTTP_AUTOLOGON_SECURITY_LEVEL_MEDIUM : Interop.WinHttp.WINHTTP_AUTOLOGON_SECURITY_LEVEL_LOW) : Interop.WinHttp.WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH; if (!Interop.WinHttp.WinHttpSetOption( requestHandle, Interop.WinHttp.WINHTTP_OPTION_AUTOLOGON_POLICY, ref optionData)) { WinHttpException.ThrowExceptionUsingLastError(); } }
public static void ResetCookieRequestHeaders(WinHttpRequestState state, Uri redirectUri) { SafeWinHttpHandle requestHandle = state.RequestHandle; Debug.Assert(state.Handler.CookieUsePolicy == CookieUsePolicy.UseSpecifiedCookieContainer); // Clear cookies. if (!Interop.WinHttp.WinHttpAddRequestHeaders( requestHandle, CookieHeaderNameWithColon, (uint)CookieHeaderNameWithColon.Length, Interop.WinHttp.WINHTTP_ADDREQ_FLAG_REPLACE)) { int lastError = Marshal.GetLastWin32Error(); if (lastError != Interop.WinHttp.ERROR_WINHTTP_HEADER_NOT_FOUND) { throw WinHttpException.CreateExceptionUsingError(lastError); } } // Re-add cookies. The GetCookieHeader() method will return the correct set of // cookies based on the redirectUri. string cookieHeader = GetCookieHeader(redirectUri, state.Handler.CookieContainer); if (!string.IsNullOrEmpty(cookieHeader)) { if (!Interop.WinHttp.WinHttpAddRequestHeaders( requestHandle, cookieHeader, (uint)cookieHeader.Length, Interop.WinHttp.WINHTTP_ADDREQ_FLAG_ADD)) { WinHttpException.ThrowExceptionUsingLastError(); } } }
private bool SetWinHttpCredential( SafeWinHttpHandle requestHandle, ICredentials credentials, Uri uri, uint authScheme, uint authTarget) { string userName; string password; Debug.Assert(credentials != null); Debug.Assert(authScheme != 0); Debug.Assert(authTarget == Interop.WinHttp.WINHTTP_AUTH_TARGET_PROXY || authTarget == Interop.WinHttp.WINHTTP_AUTH_TARGET_SERVER); NetworkCredential networkCredential = credentials.GetCredential(uri, s_authSchemeStringMapping[authScheme]); if (networkCredential == null) { return(false); } if (networkCredential == CredentialCache.DefaultNetworkCredentials) { // Only Negotiate and NTLM can use default credentials. Otherwise, // behave as-if there were no credentials. if (authScheme == Interop.WinHttp.WINHTTP_AUTH_SCHEME_NEGOTIATE || authScheme == Interop.WinHttp.WINHTTP_AUTH_SCHEME_NTLM) { // Allow WinHTTP to transmit the default credentials. ChangeDefaultCredentialsPolicy(requestHandle, authTarget, allowDefaultCredentials: true); userName = null; password = null; } else { return(false); } } else { userName = networkCredential.UserName; password = networkCredential.Password; string domain = networkCredential.Domain; // WinHTTP does not support a blank username. So, we will throw an exception. if (string.IsNullOrEmpty(userName)) { throw new InvalidOperationException("net_http_username_empty_string"); } if (!string.IsNullOrEmpty(domain)) { userName = domain + "\\" + userName; } } if (!Interop.WinHttp.WinHttpSetCredentials( requestHandle, authTarget, authScheme, userName, password, IntPtr.Zero)) { WinHttpException.ThrowExceptionUsingLastError(); } return(true); }