Пример #1
0
        private static async Task SignIn()
        {
            IConfiguration configuration = new ConfigurationBuilder()
                                           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                           .AddEnvironmentVariables()
                                           .Build();

            var scimConfig = new ScimConfig();

            configuration.Bind("scim", scimConfig);

            s_apiClient = new HttpClient()
            {
                BaseAddress = new Uri($"{scimConfig.BaseUrl.Trim('/')}/tokens/{scimConfig.Token}/")
            };

            var clientConfig = new ClientConfig();

            configuration.Bind("client", clientConfig);

            var    browser     = new SystemBrowser(5678);
            string redirectUri = string.Format($"http://127.0.0.1:5678");

            var options = new OidcClientOptions
            {
                Authority                    = clientConfig.Authority,
                ClientId                     = clientConfig.ClientId,
                RedirectUri                  = redirectUri,
                Scope                        = clientConfig.Scope,
                FilterClaims                 = false,
                Browser                      = browser,
                IdentityTokenValidator       = new JwtHandlerIdentityTokenValidator(),
                RefreshTokenInnerHttpHandler = new SocketsHttpHandler()
            };

            Serilog.Core.Logger serilog = new LoggerConfiguration()
                                          .MinimumLevel.Debug()
                                          .Enrich.FromLogContext()
                                          .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code)
                                          .CreateLogger();

            options.LoggerFactory.AddSerilog(serilog);

            s_oidcClient = new OidcClient(options);
            LoginResult result = await s_oidcClient.LoginAsync(new LoginRequest());

            ShowResult(result);
            await NextSteps(result);
        }
Пример #2
0
        private static async Task SignIn()
        {
            IConfiguration configuration = new ConfigurationBuilder()
                                           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                           .AddEnvironmentVariables()
                                           .Build();

            var scimConfig = new ScimConfig();

            configuration.Bind("scim", scimConfig);

            var scimClient = new HttpClient()
            {
                BaseAddress = new Uri($"{scimConfig.BaseUrl.Trim('/')}/tokens/{scimConfig.Token}/")
            };

            var clientConfig = new ClientConfig();

            configuration.Bind("client", clientConfig);

            var idtsClient = new HttpClient();
            var disco      = await idtsClient.GetDiscoveryDocumentAsync(clientConfig.Authority);

            if (disco.IsError)
            {
                throw new Exception(disco.Error);
            }

            TokenResponse tokenResponse = await idtsClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                Address      = disco.TokenEndpoint,
                ClientId     = clientConfig.ClientId,
                ClientSecret = clientConfig.ClientSecret,
                Scope        = clientConfig.Scope
            });

            if (tokenResponse.IsError)
            {
                Console.WriteLine(tokenResponse.Error);
                return;
            }

            Console.WriteLine(tokenResponse.Json);
            Console.WriteLine();

            // call api
            scimClient.SetBearerToken(tokenResponse.AccessToken);

            HttpResponseMessage response = await scimClient.GetAsync("scim/v2/users");

            Console.WriteLine($"{response.RequestMessage.Method} {response.RequestMessage.RequestUri}");
            Console.WriteLine($"{(int)response.StatusCode} {response.ReasonPhrase}");

            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();

                var obj = JToken.Parse(content);

                Console.WriteLine(obj.ToString(Newtonsoft.Json.Formatting.Indented));
            }
        }