Skip to content

Commit

Permalink
Not sure what changed but it appears that doing StringContent is not …
Browse files Browse the repository at this point in the history
…longer supported by AAD for getting the token
  • Loading branch information
Bert Cotton authored and Bert Cotton committed Oct 12, 2017
1 parent d8c6718 commit a6f2a48
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 57 deletions.
1 change: 0 additions & 1 deletion src/TfsAdvanced.Models/RequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public RequestData(IOptions<AppSettings> settings)

HttpClient = new HttpClient(handler);
HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationToken.base64_token);
var authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{appSettings.Security.Username}:{appSettings.Security.Password}"));
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorization);
}
Expand Down
40 changes: 20 additions & 20 deletions src/TfsAdvanced/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,30 @@ public async Task<IActionResult> ADLogin(string code = null, string state = null
return Redirect("/");
}

[HttpGet("LoginVSOAuth")]
[AllowAnonymous]
public async Task<IActionResult> LoginAuth(string code = null, string state = null, bool Admin_consent = false, string Session_state = null)
{
var tokenString = await authorizationRequest.GetVSOAccessToken(GetBaseURL(), code, state);
//[HttpGet("LoginVSOAuth")]
//[AllowAnonymous]
//public async Task<IActionResult> LoginAuth(string code = null, string state = null, bool Admin_consent = false, string Session_state = null)
//{
// var tokenString = await authorizationRequest.GetVSOAccessToken(GetBaseURL(), code, state);

var token = JsonConvert.DeserializeObject<AuthenticationToken>(tokenString);
// var token = JsonConvert.DeserializeObject<AuthenticationToken>(tokenString);

if (String.IsNullOrEmpty(token.access_token))
throw new Exception("The access token is null");
// if (String.IsNullOrEmpty(token.access_token))
// throw new Exception("The access token is null");

var cookieValue = JsonConvert.SerializeObject(token);
HttpContext.Session.Set("AuthToken", ASCIIEncoding.ASCII.GetBytes(JsonConvert.SerializeObject(token)));
HttpContext.Response.Cookies.Append("Auth", cookieValue, new CookieOptions
{
Secure = true,
Expires = DateTime.Now.AddYears(1),
HttpOnly = true,
Path = "/",
Domain = HttpContext.Request.Host.ToString()
});
// var cookieValue = JsonConvert.SerializeObject(token);
// HttpContext.Session.Set("AuthToken", ASCIIEncoding.ASCII.GetBytes(JsonConvert.SerializeObject(token)));
// HttpContext.Response.Cookies.Append("Auth", cookieValue, new CookieOptions
// {
// Secure = true,
// Expires = DateTime.Now.AddYears(1),
// HttpOnly = true,
// Path = "/",
// Domain = HttpContext.Request.Host.ToString()
// });

return Redirect("/");
}
// return Redirect("/");
//}

private string GetBaseURL()
{
Expand Down
75 changes: 39 additions & 36 deletions src/TfsAdvanced/ServiceRequests/AuthorizationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,40 @@ public AuthorizationRequest(IOptions<AppSettings> appSettings)
this.appSettings = appSettings.Value;
}

public string GetVSOChallengeUrl(string baseURL)
{
return
$"https://app.vssps.visualstudio.com/oauth2/authorize?" +
$"client_id={appSettings.authorization.AppId}&response_type=Assertion" +
$"&state={appSettings.authorization.State}&scope={appSettings.authorization.Scope}" +
$"&redirect_uri={baseURL}{appSettings.authorization.RedirectURI}";
}
//public string GetVSOChallengeUrl(string baseURL)
//{
// return
// $"https://app.vssps.visualstudio.com/oauth2/authorize?" +
// $"client_id={appSettings.authorization.AppId}&response_type=Assertion" +
// $"&state={appSettings.authorization.State}&scope={appSettings.authorization.Scope}" +
// $"&redirect_uri={baseURL}{appSettings.authorization.RedirectURI}";
//}


public async Task<string> GetVSOAccessToken(string baseURL, string code, string state)
{
var request = new HttpRequestMessage(HttpMethod.Post, "https://app.vssps.visualstudio.com/oauth2/token");
request.Content = 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", appSettings.authorization.AppSecret),
new KeyValuePair<string, string>("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"),
new KeyValuePair<string, string>("assertion", code),
new KeyValuePair<string, string>("redirect_uri", baseURL + appSettings.authorization.RedirectURI)
});
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
//public async Task<string> GetVSOAccessToken(string baseURL, string code, string state)
//{
// var request = new HttpRequestMessage(HttpMethod.Post, "https://app.vssps.visualstudio.com/oauth2/token");
// request.Content = 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", appSettings.authorization.AppSecret),
// new KeyValuePair<string, string>("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"),
// new KeyValuePair<string, string>("assertion", code),
// new KeyValuePair<string, string>("redirect_uri", baseURL + appSettings.authorization.RedirectURI)
// });
// HttpClientHandler handler = new HttpClientHandler()
// {
// AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
// };

var saveResponse = await new HttpClient(handler).SendAsync(request);
// var saveResponse = await new HttpClient(handler).SendAsync(request);

var responseText = await saveResponse.Content.ReadAsStringAsync();
return responseText;
// var responseText = await saveResponse.Content.ReadAsStringAsync();
// return responseText;

//AuthenticationToken token = JsonConvert.DeserializeObject<AuthenticationToken>(responseText);
//return token;
}
// //AuthenticationToken token = JsonConvert.DeserializeObject<AuthenticationToken>(responseText);
// //return token;
//}

public string GetADChallengeUrl(string baseURL)
{
Expand All @@ -66,20 +66,23 @@ public string GetADChallengeUrl(string baseURL)
public async Task<AuthenticationToken> GetADAccessToken(string baseURL, string code, string state)
{

var content = new StringContent($"grant_type=authorization_code&client_id={appSettings.authorization.ClientId}"+
$"&code={code}&redirect_uri={baseURL}{appSettings.authorization.RedirectURI}" +
$"&resource=https://graph.windows.net"+
$"&client_secret={appSettings.authorization.ClientSecret}",
Encoding.UTF8,
"application/x-www-form-urlencoded");
var content = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("client_id", appSettings.authorization.ClientId),
new KeyValuePair<string, string>("code", code),
new KeyValuePair<string, string>("client_secret", appSettings.authorization.ClientSecret),
new KeyValuePair<string, string>("redirect_uri", $"{baseURL}{appSettings.authorization.RedirectURI}" )
};


HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
var client = new HttpClient(handler);

var saveResponse = await client.PostAsync($"https://login.microsoftonline.com/{appSettings.authorization.TenantId}/oauth2/token", content);
var saveResponse = await client.PostAsync($"https://login.microsoftonline.com/{appSettings.authorization.TenantId}/oauth2/token", new FormUrlEncodedContent(content));

var responseText = await saveResponse.Content.ReadAsStringAsync();

Expand Down

0 comments on commit a6f2a48

Please sign in to comment.