Пример #1
0
        private static void ClientCredentialFlow()
        {
            Console.WriteLine("Processing client credential flow" + Environment.NewLine);

            var client = new OAuth2Client(new Uri(Endpoint + "/issue/oidc/token"), ClientId, ClientSecret);

            var taks = client.RequestClientCredentialsAsync(Scope);
            taks.Wait();
            var tokenResponse = taks.Result;

            Console.WriteLine("Access token received .. " + Environment.NewLine);

            Console.WriteLine(tokenResponse.AccessToken + Environment.NewLine);

            Console.WriteLine("Calling refresh token endpoint" + Environment.NewLine);

            Task<TokenResponse> refreshTokenResponse = client.RequestRefreshTokenAsync(tokenResponse.RefreshToken);
            refreshTokenResponse.Wait();
            tokenResponse = refreshTokenResponse.Result;

            Console.WriteLine("Access token received using refresh token .. " + Environment.NewLine);

            Console.WriteLine(tokenResponse.AccessToken + Environment.NewLine);

            RevokeToken(tokenResponse.AccessToken, "access_token");
        }
 static TokenResponse RefreshToken(string refreshToken)
 {
     var client = new OAuth2Client(
         new Uri("https://HFL0100:44333/connect/token"),
         "carbon",
         "21B5F798-BE55-42BC-8AA8-0025B903DC3B");
     ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
     return client.RequestRefreshTokenAsync(refreshToken).Result;
 }
Пример #3
0
        public async static Task<TokenResponse> RefreshToken(TokenResponse token)
        {
            var client = new OAuth2Client(
                 new Uri(Constant.TokenEndpoint),
                 Constant.CodeClientId,
                 Constant.CodeClientSecret);

            return await client.RequestRefreshTokenAsync(token.RefreshToken);
        }
        private static TokenResponse RefreshToken(string refreshToken)
        {
            var client = new OAuth2Client(
                new Uri("http://localhost:2727/token"),
                "client1",
                "secret");

            var response = client.RequestRefreshTokenAsync(refreshToken).Result;
            return response;
        }
        public async Task<ActionResult> RenewToken(string refreshToken)
        {
            var client = new OAuth2Client(
                new Uri(Constants.AS.OAuth2TokenEndpoint),
                Constants.Clients.CodeClient,
                Constants.Clients.CodeClientSecret);

            var response = await client.RequestRefreshTokenAsync(refreshToken);
            return View("Postback", response);
        }
        private static string RefreshToken(string refreshToken)
        {
            "Refreshing token.".ConsoleYellow();

            var client = new OAuth2Client(
                new Uri(Constants.AS.OAuth2TokenEndpoint),
                Constants.Clients.ResourceOwnerClient,
                Constants.Clients.ResourceOwnerClientSecret);

            var response = client.RequestRefreshTokenAsync(refreshToken).Result;

            return response.AccessToken;
        }
        public async Task<ActionResult> RefreshToken()
        {
            var client = new OAuth2Client(
                new Uri(Constants.TokenEndpoint),
                "codeclient",
                "secret");

            var principal = User as ClaimsPrincipal;
            var refreshToken = principal.FindFirst("refresh_token").Value;

            var response = await client.RequestRefreshTokenAsync(refreshToken);
            UpdateCookie(response);

            return RedirectToAction("Index");
        }
Пример #8
0
        private static async void CheckAndPossiblyRefreshToken(ClaimsIdentity id)
        {
            // check if the access token hasn't expired.
            if (DateTime.Now.ToLocalTime() >=
                 (DateTime.Parse(id.FindFirst("expires_at").Value)))
            {
                // expired.  Get a new one.
                var tokenEndpointClient = new OAuth2Client(
                    new Uri(ExpenseTrackerConstants.IdSrvToken),
                    "mvc",
                    "secret");

                var tokenEndpointResponse = 
                    await tokenEndpointClient
                    .RequestRefreshTokenAsync(id.FindFirst("refresh_token").Value);

                if (!tokenEndpointResponse.IsError)
                {
                    // replace the claims with the new values - this means creating a 
                    // new identity!                              
                    var result = from claim in id.Claims
                                 where claim.Type != "access_token" && claim.Type != "refresh_token" &&
                                       claim.Type != "expires_at"
                                 select claim;

                    var claims = result.ToList();

                    claims.Add(new Claim("access_token", tokenEndpointResponse.AccessToken));
                    claims.Add(new Claim("expires_at",
                                 DateTime.Now.AddSeconds(tokenEndpointResponse.ExpiresIn)
                                 .ToLocalTime().ToString()));
                    claims.Add(new Claim("refresh_token", tokenEndpointResponse.RefreshToken));

                    var newIdentity = new ClaimsIdentity(claims, "Cookies");
                    var wrapper = new HttpRequestWrapper(HttpContext.Current.Request);
                    wrapper.GetOwinContext().Authentication.SignIn(newIdentity);
                }
                else
                {
                    // log, ...
                    throw new Exception("An error has occurred");
                }
            }
             

           
        }
Пример #9
0
        public async Task<TokenResponse> RefreshToken()
        {
            var client = new OAuth2Client(
                new Uri(this.TokenEndpointAddress),
                "BSEtunes",
                OAuthClientSercret);

            this.TokenResponse = await client.RequestRefreshTokenAsync(this.TokenResponse.RefreshToken);
            return this.TokenResponse;
        }
        public IHttpActionResult RefreshToken([FromBody] RefreshTokenViewModel input)
        {
            var client = new OAuth2Client(
                new Uri("https://HFL0100:44333/connect/token"),
                "carbon",
                "21B5F798-BE55-42BC-8AA8-0025B903DC3B");

            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

            return Ok < TokenResponse >(client.RequestRefreshTokenAsync(input.Refresh_Token).Result);
        }
Пример #11
0
        private static void ResourceOwnerFlow()
        {
            Console.WriteLine("Processing Resource owner password flow");
            Console.WriteLine("");
            Console.WriteLine("");
            var client = new OAuth2Client(new Uri(Endpoint + "/issue/oidc/token"), ClientId, APIKey);
            var taks = client.RequestResourceOwnerPasswordAsync(UserName, Password, Scopes);
            taks.Wait(new TimeSpan(1, 0, 0));
            var tokenResponse = taks.Result;

            Console.WriteLine("Access token received .. ");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine(tokenResponse.AccessToken);

            Console.WriteLine("Calling user info endpoint");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");

            var httpClient = new HttpClient
            {
                BaseAddress = new Uri(Endpoint + "/issue/oidc/userinfo")
            };

            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenResponse.AccessToken);

            var response = httpClient.GetAsync("").Result;
            response.EnsureSuccessStatusCode();

            var dictionary = response.Content.ReadAsAsync<Dictionary<string, string>>().Result;
            foreach (var pair in dictionary)
            {
                Console.WriteLine(pair.Key + " ----> " + pair.Value);
            }

            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("Calling refresh token  endpoint");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
            Task<TokenResponse> refreshTokenResponse = client.RequestRefreshTokenAsync(tokenResponse.RefreshToken);
            refreshTokenResponse.Wait();
            tokenResponse = refreshTokenResponse.Result;
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("Access token received using refresh token .. ");
            Console.WriteLine("");
            Console.WriteLine(tokenResponse.AccessToken);
            Console.WriteLine("");
            Console.WriteLine("");
            RevokeToken(tokenResponse.AccessToken, "access_token");

            Console.WriteLine("");
        }