/// <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. Issuer issuer = new Issuer(Global.entityId); AuthnRequest authnRequest = Util.GetAuthnRequest(this); authnRequest.Issuer.NameIdentifier = Global.entityId; // 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. System.Diagnostics.Debug.WriteLine("Sending redirect request to " + Global.SingleSignOnServiceURL); authnRequest.Redirect(Response, Global.SingleSignOnServiceURL, relayState, x509Certificate.PrivateKey, SignatureAlgorithms.RsaSha256); break; case SamlBinding.HttpPost: // Send authentication request using HTTP POST form. System.Diagnostics.Debug.WriteLine("Sending POST request to " + Global.SingleSignOnServiceURL); 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.SendHttpPost(Response.OutputStream, Global.SingleSignOnServiceURL, relayState); // End the response. Response.End(); break; default: throw new ApplicationException("Invalid binding type"); } }
/// <summary> /// Handles the IdpLogin button to requests login at the Identify Provider site. /// </summary> /// <param name="sender">The button object.</param> /// <param name="e">The event arguments.</param> protected void btnIdPLogin_Click(object sender, EventArgs e) { // Create the authentication request. AuthnRequest authnRequest = BuildAuthenticationRequest(); // Create and cache the relay state so we remember which SP resource the user wishes // to access after SSO. string spResourceUrl = Util.GetAbsoluteUrl(this, FormsAuthentication.GetRedirectUrl("", false)); string relayState = Guid.NewGuid().ToString(); SamlSettings.CacheProvider.Insert(relayState, spResourceUrl, new TimeSpan(1, 0, 0)); // Send the authentication request to the identity provider over the selected binding. string idpUrl = string.Format("{0}?{1}={2}", WebConfigurationManager.AppSettings["SingleSignonIdProviderUrl"], Util.BindingVarName, HttpUtility.UrlEncode(spToIdPBindingList.SelectedValue)); switch (spToIdPBindingList.SelectedValue) { case SamlBindingUri.HttpRedirect: X509Certificate2 x509Certificate = (X509Certificate2)Application[Global.SPCertKey]; authnRequest.Redirect(Response, idpUrl, relayState, x509Certificate.PrivateKey); break; case SamlBindingUri.HttpPost: authnRequest.SendHttpPost(Response, idpUrl, relayState); // Don't send this form. Response.End(); break; case SamlBindingUri.HttpArtifact: // Create the artifact. string identificationUrl = Util.GetAbsoluteUrl(this, "~/"); Saml2ArtifactType0004 httpArtifact = new Saml2ArtifactType0004(SamlArtifact.GetSourceId(identificationUrl), SamlArtifact.GetHandle()); // Cache 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. httpArtifact.Redirect(Response, idpUrl, relayState); break; } }