private NAMResponse LoginWithCertificateAndSAMLRequest(Uri loginUri, string samlRequest) { var certificate = GetDeviceCertificate(); Debug.WriteLine("Certificate found, verifying access to private key..."); var verifyResult = VerifyCertificateKeyAccess(certificate); if (verifyResult) { Debug.WriteLine("Access granted, creating HttpClient..."); HttpBaseProtocolFilter httpFilter = new HttpBaseProtocolFilter(); httpFilter.ClientCertificate = certificate; // try login HttpClient httpClient = new HttpClient(httpFilter); try { Debug.WriteLine("Sending SAML request to NAM..."); var uriBuilder = new UriBuilder(loginUri); if (samlRequest != null) { uriBuilder.Query = "SAMLRequest=" + WebUtility.UrlEncode(samlRequest); } var ssoResponseTask = httpClient.GetAsync(uriBuilder.Uri).AsTask(); ssoResponseTask.Wait(); var ssoResponse = ssoResponseTask.Result; ssoResponse.EnsureSuccessStatusCode(); // follow the Javascript redirect Debug.WriteLine("Received response, trying to extract redirect URI from javascript"); var redirectUri = ExtractJavaScriptRedirectUri(ssoResponse, loginUri); Debug.WriteLine("Requesting SAML response from: " + redirectUri.ToString()); var redirectResponseTask = httpClient.GetAsync(redirectUri).AsTask(); redirectResponseTask.Wait(); var redirectResponse = redirectResponseTask.Result; redirectResponse.EnsureSuccessStatusCode(); // read the SAMLResponse Debug.WriteLine("Received response from NAM, extracting SAMLResponse..."); NAMResponse namResponse = new NAMResponse(redirectResponse); Debug.WriteLine("Returning SAML Response: " + namResponse.SAMLResponse); string cookieHeader; ssoResponse.Headers.TryGetValue("Set-Cookie", out cookieHeader); namResponse.Cookies = cookieHeader; Debug.WriteLine("And Cookies: " + namResponse.Cookies); // update the session timestamp var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; localSettings.Values[SESSION_TIMESTAMP_KEY] = DateTime.Now.ToString(); return(namResponse); } catch (FormatException formatException) { Debug.WriteLine("Uri extraction error: " + formatException.Message); return(null); } catch (Exception ex) { Debug.WriteLine("Connection error..."); Debug.WriteLine("Exception during NAM communication: " + ex.Message); var exceptionDetail = WebError.GetStatus(ex.GetBaseException().HResult); Debug.WriteLine("Exception Detail: " + exceptionDetail); return(null); } } return(null); }
private NAMResponse LoginDirectlyWithCertificateOnly() { var certificate = GetDeviceCertificate(); Debug.WriteLine("Certificate found, verifying access to private key..."); var verifyResult = VerifyCertificateKeyAccess(certificate); if (verifyResult) { Debug.WriteLine("Access granted, creating HttpClient..."); HttpBaseProtocolFilter httpFilter = new HttpBaseProtocolFilter(); httpFilter.ClientCertificate = certificate; // try login HttpClient httpClient = new HttpClient(httpFilter); try { Debug.WriteLine("Authenticating directly with NAM..."); var loginUri = new Uri("https://login.schaeffler.com/nidp/app/login?id=Smartcard"); var ssoResponseTask = httpClient.GetAsync(loginUri).AsTask(); ssoResponseTask.Wait(); var ssoResponse = ssoResponseTask.Result; ssoResponse.EnsureSuccessStatusCode(); // follow the Javascript redirect Debug.WriteLine("Received response, trying to extract redirect URI from javascript"); var redirectUri = ExtractJavaScriptRedirectUri(ssoResponse, loginUri); Debug.WriteLine("Requesting SAML response from: " + redirectUri.ToString()); var redirectResponseTask = httpClient.GetAsync(redirectUri).AsTask(); redirectResponseTask.Wait(); var redirectResponse = redirectResponseTask.Result; redirectResponse.EnsureSuccessStatusCode(); // create an empty Response object, since the NAM won't send a SAMLResponse when authenticating directly NAMResponse namResponse = new NAMResponse(); namResponse.SAMLResponse = ""; string cookieHeader; ssoResponse.Headers.TryGetValue("Set-Cookie", out cookieHeader); namResponse.Cookies = cookieHeader; Debug.WriteLine("Cookies: " + namResponse.Cookies); // update the session timestamp var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; localSettings.Values[SESSION_TIMESTAMP_KEY] = DateTime.Now.ToString(); return(namResponse); } catch (FormatException formatException) { Debug.WriteLine("Uri extraction error: " + formatException.Message); return(null); } catch (Exception ex) { Debug.WriteLine("Connection error..."); Debug.WriteLine("Exception during NAM communication: " + ex.Message); var exceptionDetail = WebError.GetStatus(ex.GetBaseException().HResult); Debug.WriteLine("Exception Detail: " + exceptionDetail); return(null); } } return(null); }