using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace ConsoleResourceOwnerFlowRefreshToken
{
public class Program
{
public static void Main(string[] args) => MainAsync().GetAwaiter().GetResult();
private static async Task MainAsync()
{
string tokenEndpoint = null;
while (tokenEndpoint == null)
{
Console.Clear();
Console.WriteLine("Get TokenEndpoint");
try
{
tokenEndpoint = await GetTokenEndpointUri("https://localhost:5000/.well-known/openid-configuration");
}
catch (Exception e)
{
Console.WriteLine();
Console.WriteLine($"Error: {e}");
Console.WriteLine();
Console.WriteLine("Press q to quit or any other key to retry");
if (char.ToLower(Console.ReadKey().KeyChar) == 'q')
return;
tokenEndpoint = null;
}
}
var tokenRequest = new TokenRequest
{
TokenEndppointUri = tokenEndpoint,
Scopes = new[] { "openid", "c1s_api", "roles" },
ClientIdentifier = "anyClient",
ClientSecret = "anySecret",
Username = "someUserName",
Password = "someUserPassword"
};
do
{
Console.Clear();
try
{
Console.WriteLine($"Token-Endpoint: {tokenEndpoint}");
Console.WriteLine();
var token = await GetToken(tokenRequest);
Console.WriteLine(token ?? "null");
}
catch (Exception e)
{
Console.WriteLine($"Error: {e}");
}
Console.WriteLine();
Console.WriteLine("Press q to quit or any other key to renew token");
} while (char.ToLower(Console.ReadKey().KeyChar) != 'q');
}
private static async Task<string> GetToken(TokenRequest tokenRequest)
{
using (var httpClient = new HttpClient())
{
//Use Client-Identifier / Client-Secret for Basic-Authentication
httpClient.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue(tokenRequest.ClientIdentifier, tokenRequest.ClientSecret);
//Create a POST request to the token-endpoint with data in FormUrlEncoded format
var tokenResponse = await httpClient.PostAsync(tokenRequest.TokenEndppointUri, new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type" , "password"},
{"username", tokenRequest.Username },
{"password", tokenRequest.Password },
{"scope", string.Join(" ", tokenRequest.Scopes)}
}));
//return the property access_token from POST-Response
var tokenResponseDoc = await tokenResponse.Content.ReadAsStringAsync();
var tokenElement = (dynamic)JsonConvert.DeserializeObject(tokenResponseDoc);
return (string)tokenElement.access_token;
}
}
private static async Task<string> GetTokenEndpointUri(string discoUri)
{
using (var httpClient = new HttpClient())
{
//Create a simple GET-Request to the Discovery-Uri
var discoDoc = await httpClient.GetStringAsync(discoUri);
//return the property token_endpoint from GET-Response
var discoElement = (dynamic)JsonConvert.DeserializeObject(discoDoc);
return (string)discoElement.token_endpoint;
}
}
}
public class TokenRequest
{
public string TokenEndppointUri { get; set; }
public string ClientIdentifier { get; set; }
public string ClientSecret { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string[] Scopes { get; set; }
}
} |