Ejemplo n.º 1
0
        async void HandleCallback(string url)
        {
            await safari.DismissViewControllerAsync(true);

            var result = await _client.ValidateResponseAsync(url, _state);

            var sb = new StringBuilder(128);

            foreach (var claim in result.Claims)
            {
                sb.AppendFormat("{0}: {1}\n", claim.Type, claim.Value);
            }

            sb.AppendFormat("\n{0}: {1}\n", "refresh token", result.RefreshToken);
            sb.AppendFormat("\n{0}: {1}\n", "access token", result.AccessToken);

            OutputTextView.Text    = sb.ToString();
            _apiClient             = new HttpClient(result.Handler);
            _apiClient.BaseAddress = new Uri("https://demo.identityserver.io/api/");
        }
        private async void SignIn()
        {
            // create a redirect URI using an available port on the loopback address.
            string redirectUri = string.Format("http://127.0.0.1:7890/");

            Console.WriteLine("redirect URI: " + redirectUri);

            // create an HttpListener to listen for requests on that redirect URI.
            var http = new HttpListener();

            http.Prefixes.Add(redirectUri);
            Console.WriteLine("Listening..");
            http.Start();

            var options = new OidcClientOptions(
                "https://demo.identityserver.io",
                "native",
                "secret",
                "openid profile api",
                redirectUri)
            {
                UseFormPost = true,
                Style       = OidcClientOptions.AuthenticationStyle.Hybrid
            };

            var client = new OidcClient(options);
            var state  = await client.PrepareLoginAsync();

            Console.WriteLine($"Start URL: {state.StartUrl}");

            // open system browser to start authentication
            Process.Start(state.StartUrl);

            // wait for the authorization response.
            var context = await http.GetContextAsync();

            var formData = GetRequestPostData(context.Request);

            // Brings the Console to Focus.
            BringConsoleToFront();

            // sends an HTTP response to the browser.
            var    response       = context.Response;
            string responseString = string.Format("<html><head><meta http-equiv='refresh' content='10;url=https://demo.identityserver.io'></head><body>Please return to the app.</body></html>");
            var    buffer         = Encoding.UTF8.GetBytes(responseString);

            response.ContentLength64 = buffer.Length;
            var responseOutput = response.OutputStream;
            await responseOutput.WriteAsync(buffer, 0, buffer.Length);

            responseOutput.Close();

            Console.WriteLine($"Form Data: {formData}");
            var result = await client.ValidateResponseAsync(formData, state);

            if (result.Success)
            {
                Console.WriteLine("\n\nClaims:");
                foreach (var claim in result.Claims)
                {
                    Console.WriteLine("{0}: {1}", claim.Type, claim.Value);
                }

                Console.WriteLine();
                Console.WriteLine("Access token:\n{0}", result.AccessToken);

                if (!string.IsNullOrWhiteSpace(result.RefreshToken))
                {
                    Console.WriteLine("Refresh token:\n{0}", result.RefreshToken);
                }
            }
            else
            {
                Console.WriteLine("\n\nError:\n{0}", result.Error);
            }

            http.Stop();
        }