示例#1
0
        private static void TestaGetToken()
        {
            string stToken   = string.Empty;
            var    stopwatch = new System.Diagnostics.Stopwatch();

            Console.Write("Aguarde...");

            try
            {
                stopwatch.Start();
                TokenData objToken = TokenApiClient.GetToken(userName, password, apiBaseUri);
                stopwatch.Stop();

                stToken = string.Format("client_id = {0}{5}access_token = {1}{5}expires_at = {2}{5}seconds_left = {3}{5}success = {4}",
                                        objToken.client_id, objToken.access_token, objToken.expires_at, objToken.seconds_left, objToken.success, Environment.NewLine);
            }
            catch (Exception ex)
            {
                stToken = ex.Message;
            }

            Console.Clear();
            Console.WriteLine(stToken);
            Console.WriteLine();
            Console.WriteLine($"Tempo de execução do método GetToken: {stopwatch.Elapsed}");
            stopwatch.Restart();
        }
        public ActionResult AssertionConsumerService()
        {
            bool   isInResponseTo = false;
            string partnerIdP     = null;
            string userName       = null;
            IDictionary <string, string> attributes = null;
            string targetUrl = null;

            byte[] bytes           = Encoding.Default.GetBytes(Request.Form[0]);
            String samlResponseStr = Encoding.UTF8.GetString(bytes);

            samlResponseStr = Encoding.Default.GetString(Convert.FromBase64String(samlResponseStr));

            Regex regex = new Regex("<saml2:Assertion(.*)?</saml2:Assertion>");
            Match match = regex.Match(samlResponseStr);

            if (!match.Success)
            {
                throw new Exception("SAML assertion not found in response: " + samlResponseStr);
            }

            TokenApiClient tokenClient    = new TokenApiClient();
            String         samlAssertion  = match.Value;
            String         apiAccessToken = tokenClient.GetAccessToken(samlAssertion);

            if (String.IsNullOrEmpty(apiAccessToken))
            {
                throw new Exception("Could not retrieve API access token");
            }
            Runtime.ApiAccessToken = apiAccessToken;

            try
            {
                // Receive and process the SAML assertion contained in the SAML response.
                // The SAML response is received either as part of IdP-initiated or SP-initiated SSO.
                SAMLServiceProvider.ReceiveSSO(Request, out isInResponseTo, out partnerIdP, out userName, out attributes, out targetUrl);
            }
            catch (Exception e)
            {
                if ((e.Message != null) && (e.Message.Contains("The XML is not a SAMLResponse")))
                {
                    // Due to some reason SAMLResponse of the logout is not properly processed
                    return(RedirectToLocal(targetUrl));
                }
            }

            // If no target URL is provided, provide a default.
            if (targetUrl == null)
            {
                targetUrl = "~/";
            }

            // Login automatically using the asserted identity.
            // This example uses forms authentication. Your application can use any authentication method you choose.
            // There are no restrictions on the method of authentication.
            FormsAuthentication.SetAuthCookie(userName, false);

            // Save the attributes.
            Session[AttributesSessionKey] = attributes;

            // Redirect to the target URL.
            return(RedirectToLocal(targetUrl));
        }