Exemplo n.º 1
0
        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;
            }
        }