Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created March 12, 2024 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/3debaac2d80be2826b2db1c6eba0f814 to your computer and use it in GitHub Desktop.
Save bjoerntx/3debaac2d80be2826b2db1c6eba0f814 to your computer and use it in GitHub Desktop.
[HttpGet]
public string Token()
{
string code = Request.Query["code"];
// Security credentials
string clientId = "";
string clientSecret = "";
string serviceUrl = "https://trial.dsserver.io";
string authorizationCode = "authorization_code";
BearerToken token;
using (HttpClient client = new HttpClient())
{
// Generate the payload
var payload = new Dictionary<string, string>
{
["grant_type"] = authorizationCode,
["code"] = code,
["client_id"] = clientId,
["client_secret"] = clientSecret,
["redirect_uri"] = "https://localhost:7194/Home/Token",
};
// Token endpoint
string requestUri = $"{serviceUrl}/oauth/token";
// Create the request message
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, requestUri)
{
Content = new StringContent(
UrlEncode(payload),
Encoding.UTF8,
"application/x-www-form-urlencoded")
};
// Send the request
var tokenResponse = client.SendAsync(tokenRequest).Result;
// Retrieve and return the token
var tokenResponseStream = tokenResponse.Content.ReadAsStringAsync().Result;
token = JsonConvert.DeserializeObject<BearerToken>(tokenResponseStream);
}
return token.AccessToken;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment