Added Refresh Token
This commit is contained in:
parent
3f3eadb3a9
commit
a2e55cd406
2 changed files with 38 additions and 19 deletions
|
@ -1,4 +1,5 @@
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using System.Text.Json;
|
||||||
using TestTokenCreator.Models;
|
using TestTokenCreator.Models;
|
||||||
|
|
||||||
namespace TestTokenCreator.Controllers
|
namespace TestTokenCreator.Controllers
|
||||||
|
@ -36,33 +37,38 @@ namespace TestTokenCreator.Controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet(Name = "oauth-callback")]
|
[HttpGet(Name = "oauth-callback")]
|
||||||
public async Task<string> OAuthCallback()
|
public async Task<TokenModel> OAuthCallback()
|
||||||
{
|
{
|
||||||
// https://testpatcreation.azurewebsites.net/Token/OAuthCallback?code=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Im9PdmN6NU1fN3AtSGpJS2xGWHo5M3VfVjBabyJ9.eyJhdWkiOiI1MmI2MmJmNC1lOTdhLTQ3ZDctOThhMi03OWViNDEwOTdmOGYiLCJuYW1laWQiOiJjZmQ2Nzg2NC03ZTY5LTZjODMtYTEzNS04YzU4NzNkYTZjODkiLCJzY3AiOiJ2c28uY29kZV9mdWxsIHZzby50b2tlbmFkbWluaXN0cmF0aW9uIHZzby50b2tlbnMgdnNvLmF1dGhvcml6YXRpb25fZ3JhbnQiLCJpc3MiOiJhcHAudnN0b2tlbi52aXN1YWxzdHVkaW8uY29tIiwiYXVkIjoiYXBwLnZzdG9rZW4udmlzdWFsc3R1ZGlvLmNvbSIsIm5iZiI6MTY1MTA2NDEwNywiZXhwIjoxNjUxMDY1MDA3fQ.Aveuz8sKpKBrj1x5Ck8ODUERuOjIIlvJ_3FIUGJMeH4gUTAEAV69YNcIqXzOo2slyt4Kuwy5Ot3-LaiUJFVpM-2OGb3sNvrunMoBccGiJpOBsi-QruabViSMPyVGSRIgP9oj_QrixUK70Q6Jzx5BmIgeay_z0jkuxAkKdlmRVExJ7mDkq_cVFvKmhFBUIB_NFPr20uDfBg0Bi9wra9MUpv4hBeTqia-K65ARmbA0g2EwAt3_na0kkL35dUggVofyjLFQPwcKiUIWOe-RJAKPQTBEoIuHVGrV2mf1fphZ561Bm6QTepMXq47YDibSGlbzr0tJAJ2Z786YbkMgqrJAEg&state=User1
|
string code = HttpContext.Request.Query["code"].ToString();
|
||||||
try {
|
string state = HttpContext.Request.Query["state"].ToString();
|
||||||
string code = HttpContext.Request.Query["code"].ToString();
|
Console.WriteLine($"Code: {code}");
|
||||||
string state = HttpContext.Request.Query["state"].ToString();
|
Console.WriteLine($"State: {state}");
|
||||||
Console.WriteLine($"Code: {code}");
|
string grantType = "urn:ietf:params:oauth:grant-type:jwt-bearer";
|
||||||
Console.WriteLine($"State: {state}");
|
HttpContent content = GenerateRequestPostData(DataModel.Instance.Secret, grantType, code, DataModel.Instance.RedirectUri);
|
||||||
|
HttpResponseMessage response = await client.PostAsync("https://app.vssps.visualstudio.com/oauth2/token", content);
|
||||||
HttpContent content = GenerateRequestPostData(DataModel.Instance.Secret, code, DataModel.Instance.RedirectUri);
|
string resp = await response.Content.ReadAsStringAsync();
|
||||||
HttpResponseMessage response = await client.PostAsync("https://app.vssps.visualstudio.com/oauth2/token", content);
|
TokenModel model = JsonSerializer.Deserialize<TokenModel>(resp);
|
||||||
string resp = await response.Content.ReadAsStringAsync();
|
return model;
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return "Caught exception : " + ex.Message;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpContent GenerateRequestPostData(string appSecret, string authCode, string callbackUrl)
|
[HttpGet(Name = "RefreshToken")]
|
||||||
|
public async Task<TokenModel> RefreshToken(string refreshToken)
|
||||||
|
{
|
||||||
|
string grantType = "refresh_token";
|
||||||
|
HttpContent content = GenerateRequestPostData(DataModel.Instance.Secret, grantType, refreshToken, DataModel.Instance.RedirectUri);
|
||||||
|
HttpResponseMessage response = await client.PostAsync("https://app.vssps.visualstudio.com/oauth2/token", content);
|
||||||
|
string resp = await response.Content.ReadAsStringAsync();
|
||||||
|
TokenModel model = JsonSerializer.Deserialize<TokenModel>(resp);
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static HttpContent GenerateRequestPostData(string appSecret, string grantType, string authCode, string callbackUrl)
|
||||||
{
|
{
|
||||||
return new FormUrlEncodedContent(new[]
|
return new FormUrlEncodedContent(new[]
|
||||||
{
|
{
|
||||||
new KeyValuePair<string, string>("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"),
|
new KeyValuePair<string, string>("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"),
|
||||||
new KeyValuePair<string, string>("client_assertion", Uri.EscapeUriString(appSecret)),
|
new KeyValuePair<string, string>("client_assertion", Uri.EscapeUriString(appSecret)),
|
||||||
new KeyValuePair<string, string>("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"),
|
new KeyValuePair<string, string>("grant_type", grantType),
|
||||||
new KeyValuePair<string, string>("assertion", Uri.EscapeUriString(authCode)),
|
new KeyValuePair<string, string>("assertion", Uri.EscapeUriString(authCode)),
|
||||||
new KeyValuePair<string, string>("redirect_uri", callbackUrl)
|
new KeyValuePair<string, string>("redirect_uri", callbackUrl)
|
||||||
});
|
});
|
||||||
|
|
13
TestTokenCreator/Models/TokenModel.cs
Normal file
13
TestTokenCreator/Models/TokenModel.cs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
namespace TestTokenCreator.Models
|
||||||
|
{
|
||||||
|
public class TokenModel
|
||||||
|
{
|
||||||
|
public string access_token { get; set; }
|
||||||
|
|
||||||
|
public string token_type { get; set; }
|
||||||
|
|
||||||
|
public string expire_in { get; set; }
|
||||||
|
|
||||||
|
public string refresh_token { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue