/// <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; } }