Beispiel #1
0
        private static async Task SignIn()
        {
            // create a redirect URI using an available port on the loopback address.
            // requires the OP to allow random ports on 127.0.0.1 - otherwise set a static port
            var    browser     = new SystemBrowser();
            string redirectUri = string.Format($"http://127.0.0.1:{browser.Port}");

            var options = new OidcClientOptions
            {
                Authority = Constants.Authority,

                ClientId = "console.pkce",

                RedirectUri  = redirectUri,
                Scope        = "openid profile feature1",
                FilterClaims = false,
                Browser      = browser
            };

            var serilog = new LoggerConfiguration()
                          .MinimumLevel.Error()
                          .Enrich.FromLogContext()
                          .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}{Exception}{NewLine}")
                          .CreateLogger();

            options.LoggerFactory.AddSerilog(serilog);

            _oidcClient = new OidcClient(options);
            var result = await _oidcClient.LoginAsync(new LoginRequest());

            ShowResult(result);
            await NextSteps(result);
        }
Beispiel #2
0
        /// <summary>
        /// Command-line client for OpenID Connect
        /// </summary>
        /// <param name="a">The authority (required)</param>
        /// <param name="c">The client ID (required)</param>
        /// <param name="s">The scope (defaults to 'openid')</param>
        /// <param name="p">The callback port (defaults to a random port)</param>
        ///<param name="d">Enables diagnostics</param>
        /// <param name="clientSecret">The client secret (optional)</param>
        static async Task <int> Main(string a, string c, string s = "openid", int p = 0, bool d = false, string clientSecret = null)
        {
            if (string.IsNullOrEmpty(a))
            {
                Console.WriteLine("authority is required. Use -h for help.");
                return(1);
            }

            if (string.IsNullOrEmpty(c))
            {
                Console.WriteLine("client id is required. Use -h for help.");
                return(1);
            }

            SystemBrowser browser;

            if (p == 0)
            {
                browser = new SystemBrowser();
            }
            else
            {
                browser = new SystemBrowser(p);
            }

            var options = new OidcClientOptions
            {
                Authority    = a,
                ClientId     = c,
                RedirectUri  = $"http://127.0.0.1:{browser.Port}",
                Scope        = s,
                FilterClaims = false,
                Browser      = browser,
                Flow         = OidcClientOptions.AuthenticationFlow.AuthorizationCode,
                ResponseMode = OidcClientOptions.AuthorizeResponseMode.Redirect,
                ClientSecret = clientSecret,
            };

            if (d)
            {
                var serilog = new LoggerConfiguration()
                              .MinimumLevel.Verbose()
                              .Enrich.FromLogContext()
                              .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}{Exception}{NewLine}")
                              .CreateLogger();

                options.LoggerFactory.AddSerilog(serilog);
            }

            var oidcClient = new OidcClient(options);
            var result     = await oidcClient.LoginAsync(new LoginRequest());

            return(ShowResult(result));
        }
Beispiel #3
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);
        }
        private static async Task SignIn()
        {
            // create a redirect URI using an available port on the loopback address.
            // requires the OP to allow random ports on 127.0.0.1 - otherwise set a static port
            var    browser     = new SystemBrowser();
            string redirectUri = string.Format($"http://127.0.0.1:{browser.Port}");

            var options = new OidcClientOptions
            {
                Authority    = _authority,
                ClientId     = "interactive.public",
                RedirectUri  = redirectUri,
                Scope        = "openid profile api offline_access",
                FilterClaims = false,

                Browser = browser,
                IdentityTokenValidator       = new JwtHandlerIdentityTokenValidator(),
                RefreshTokenInnerHttpHandler = new HttpClientHandler()
            };

            var 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);

            _oidcClient = new OidcClient(options);
            var result = await _oidcClient.LoginAsync(new LoginRequest());

            _apiClient = new HttpClient(result.RefreshTokenHandler)
            {
                BaseAddress = new Uri(_api)
            };

            ShowResult(result);
            await NextSteps(result);
        }
Beispiel #5
0
        private static async Task Login(OidcClientOptions options)
        {
            // create a redirect URI using an available port on the loopback address. requires the
            // OP to allow random ports on 127.0.0.1 - otherwise set a static port
            var    browser     = new SystemBrowser(3000);
            string redirectUri = string.Format($"http://127.0.0.1:{browser.Port}");

            //string redirectUri = "https://keycloak.docufy.de/auth/realms/AzureAdTest/broker/oidc/endpoint/auth";

            //var options = new OidcClientOptions
            //{
            //    Authority = _authority,
            //    ClientId = "native.code",
            //    RedirectUri = redirectUri,
            //    Scope = "openid profile api",
            //    FilterClaims = false,
            //    Browser = browser,
            //    Flow = OidcClientOptions.AuthenticationFlow.AuthorizationCode,
            //    ResponseMode = OidcClientOptions.AuthorizeResponseMode.Redirect
            //};

            options.Browser     = browser;
            options.RedirectUri = redirectUri;

            var serilog = new LoggerConfiguration()
                          .MinimumLevel.Error()
                          .Enrich.FromLogContext()
                          .WriteTo.LiterateConsole(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message}{NewLine}{Exception}{NewLine}")
                          .CreateLogger();

            options.LoggerFactory.AddSerilog(serilog);

            _oidcClient = new OidcClient(options);
            var result = await _oidcClient.LoginAsync(new LoginRequest());

            ShowResult(result);
            await NextSteps(result);
        }