public ActionResult Index(string scopes)
        {
            var client = new OAuth2Client(new Uri(Constants.AuthorizeEndpoint));
            var url = client.CreateCodeFlowUrl(
                "codeclient",
                scopes,
                "https://localhost:44312/callback",
                "123");

            return Redirect(url);
        }
        public ActionResult Index()
        {
            var client = new OAuth2Client(new Uri(Constants.AS.OAuth2AuthorizeEndpoint));

            var url = client.CreateCodeFlowUrl(
                Constants.Clients.CodeClient,
                "read search",
                Constants.Clients.CodeClientRedirectUrl);
            
            ViewBag.AuthorizeUrl = url;

            return View();
        }
        public ActionResult Index(string scopes)
        {
            var state = Guid.NewGuid().ToString("N");
            var nonce = Guid.NewGuid().ToString("N");
            SetTempState(state, nonce);

            var client = new OAuth2Client(new Uri(Constants.AuthorizeEndpoint));
            
            var url = client.CreateCodeFlowUrl(
                clientId:    "codeclient",
                scope:        scopes,
                redirectUri: "https://localhost:44312/callback",
                state:       state,
                nonce:       nonce);

            return Redirect(url);
        }
Пример #4
0
        private static void RequestAuthorizationCode()
        {
            var authClient = new OAuth2Client(
                new Uri(Constant.TokenEndpointAuthorize),
                Constant.CodeClientId,
                Constant.CodeClientSecret);

            var state = Util.WriteState();
            var requestUrl = authClient.CreateCodeFlowUrl(
                Constant.CodeClientId,
                Constant.Scope,
                Constant.RedirectUriApi,
                state);

            System.Diagnostics.Process.Start(requestUrl);
        }
Пример #5
0
        public void Configure(IApplicationBuilder app)
        {
            app.UseCookieAuthentication(options =>
            {
                options.AuthenticationScheme = "Cookies";
                options.AutomaticAuthentication = true;
            });

            app.Map("/code", application =>
            {
                application.Use((context, next) =>
                {

                    var client = new OAuth2Client(new Uri("http://localhost:5000/core/connect/authorize"));

                    var url = client.CreateCodeFlowUrl(clientId: "0011FF", redirectUri: "http://localhost:5001/callback", scope: "read write");

                    context.Response.Redirect(url);

                    return Task.FromResult(0);

                });
            });

            app.Map("/callback", application =>
            {
                application.Use(next => async context =>
               {
                   var client = new OAuth2Client(new Uri("http://localhost:5000/core/connect/token"), "0011FF", "ABCDEFG");

                   var code = context.Request.Query["code"];

                   TokenResponse response = await client.RequestAuthorizationCodeAsync(code, "http://localhost:5001/callback");

                   if (!string.IsNullOrEmpty(response.AccessToken))
                   {
                       List<Claim> claims = new List<Claim>();
                       claims.Add(new Claim("access_token", response.AccessToken));
                       claims.Add(new Claim("expires_at", (DateTime.UtcNow.ToEpochTime() + response.ExpiresIn).ToDateTimeFromEpoch().ToString()));

                       ClaimsIdentity id = new ClaimsIdentity(claims, "cookie");
                       ClaimsPrincipal principal = new ClaimsPrincipal(id);

                       context.Response.SignIn("Cookies", principal);
                   }
               });
            });

            app.Map("/info", application =>
            {
                application.Use(next => async context =>
                 {
                     ClaimsPrincipal principal = context.User;

                     await context.Response.WriteAsync(principal.FindFirst("access_token").Value);
                 });
            });

            app.Map("/call", application =>
            {
                application.Use(next => async context =>

                {
                     HttpClient client = new HttpClient();

                     string accessToken = context.User.FindFirst("access_token").Value;

                     client.SetBearerToken(accessToken);

                     string response = await client.GetStringAsync("http://localhost:5002/action");
                    await context.Response.WriteAsync(response);

                });
            });
        }
Пример #6
0
        private string CreateAuthUrl(OAuth2Client authClient)
        {
            var requestUrl = authClient.CreateCodeFlowUrl(
                Constant.CodeClientId,
                Constant.Scope,
                Constant.RedirectUriCode);

            Console.WriteLine("Request Authorization code:\n" + requestUrl);

            return requestUrl;
        }