/// <summary> /// Sends the SAML response to the Service Provider. /// </summary> /// <param name="samlResponse">The SAML response object.</param> /// <param name="relayState">The relay state.</param> public static void SendResponse(Page page, ComponentPro.Saml2.Response samlResponse, string relayState) { // Sign the SAML response. X509Certificate2 x509Certificate = (X509Certificate2)page.Application[Global.IdPCertKey]; samlResponse.Sign(x509Certificate); switch (Global.AssertionServiceSamlBinding) { case SamlBinding.HttpPost: // Send the SAML Response object. samlResponse.SendPostBindingForm(page.Response.OutputStream, Global.AssertionServiceUrl, relayState); break; case SamlBinding.HttpArtifact: // Create the artifact. string identificationUrl = GetAbsoluteUrl(page, "~/"); Saml2ArtifactType0004 httpArtifact = new Saml2ArtifactType0004(SamlArtifact.GetSourceId(identificationUrl), SamlArtifact.GetHandle()); // Convert the authentication request to XML and save to the application Cache. SamlSettings.CacheProvider.Insert(httpArtifact.ToString(), samlResponse.GetXml(), new TimeSpan(1, 0, 0)); // Send the artifact with POST form. httpArtifact.SendPostForm(page.Response.OutputStream, Global.AssertionServiceUrl, relayState); break; default: throw new ApplicationException("Invalid assertion consumer service binding."); } }
// Send the SAML response over the specified binding. public static void SendSamlResponse(Page page, ComponentPro.Saml2.Response samlResponse, SsoAuthnState ssoState) { // Sign the SAML response X509Certificate2 x509Certificate = (X509Certificate2)page.Application[Global.IdPCertKey]; samlResponse.Sign(x509Certificate); // Send the SAML response to the service provider. switch (ssoState.IdpProtocolBinding) { case SamlBinding.HttpPost: samlResponse.SendPostBindingForm(page.Response.OutputStream, ssoState.AssertionConsumerServiceURL, ssoState.RelayState); break; case SamlBinding.HttpArtifact: // Create the artifact. string identificationUrl = Util.GetAbsoluteUrl(page, "~/"); Saml2ArtifactType0004 httpArtifact = new Saml2ArtifactType0004(SamlArtifact.GetSourceId(identificationUrl), SamlArtifact.GetHandle()); // Cache the authentication request for subsequent sending using the artifact resolution protocol. Sliding expiration time is 1 hour. SamlSettings.CacheProvider.Insert(httpArtifact.ToString(), samlResponse.GetXml(), new TimeSpan(1, 0, 0)); // Send the artifact. httpArtifact.SendPostForm(page.Response.OutputStream, ssoState.AssertionConsumerServiceURL, ssoState.RelayState); break; default: Trace.Write("IdentityProvider", "Invalid identity provider binding"); break; } }
protected void btnIdPLogin_Click(object sender, EventArgs e) { // Get the authentication request. AuthnRequest authnRequest = Util.GetAuthnRequest(this); // Get SP Resource URL. string spResourceUrl = Util.GetAbsoluteUrl(this, FormsAuthentication.GetRedirectUrl("", false)); // Create relay state. string relayState = Guid.NewGuid().ToString(); // Save the SP Resource URL to the cache. SamlSettings.CacheProvider.Insert(relayState, spResourceUrl, new TimeSpan(1, 0, 0)); switch (Global.SingleSignOnServiceBinding) { case SamlBinding.HttpRedirect: X509Certificate2 x509Certificate = (X509Certificate2)Application[Global.SpCertKey]; // Send authentication request using HTTP Redirect. authnRequest.Redirect(Response, Global.SingleSignOnServiceURL, relayState, x509Certificate.PrivateKey); break; case SamlBinding.HttpPost: // Send authentication request using HTTP POST form. authnRequest.SendHttpPost(Response, Global.SingleSignOnServiceURL, relayState); // End the response. Response.End(); break; case SamlBinding.HttpArtifact: // Create a new http artifact. string identificationUrl = Util.GetAbsoluteUrl(this, "~/"); Saml2ArtifactType0004 httpArtifact = new Saml2ArtifactType0004(SamlArtifact.GetSourceId(identificationUrl), SamlArtifact.GetHandle()); // Save the authentication request for subsequent sending using the artifact resolution protocol. SamlSettings.CacheProvider.Insert(httpArtifact.ToString(), authnRequest.GetXml(), new TimeSpan(1, 0, 0)); // Send the artifact using HTTP POST form. httpArtifact.SendPostForm(Response.OutputStream, Global.SingleSignOnServiceURL, relayState); // End the response. Response.End(); break; default: throw new ApplicationException("Invalid binding type"); } }