I came up with a working solution and wanted to share it with the community. I am not sure of the protocol for answering my question, based on helpful reviews from other authors (Joël Franusic). If I break the protocol, let me know.
Thanks to Joël Franusic for pointers. I implemented its solution 1.2 (User Agent with Okta client). Between his links and several other documents on the Okta website, I managed to eventually combine the working code.
private static async Task<string> GetTestSamlResponse()
{
try
{
string username = "USERNAME GOES HERE";
string password = "PASSWORD GOES HERE";
var apiToken = "API TOKEN GOES HERE";
var baseUrl = "YOUR BASE URL GOES HERE";
var ssoUrl = "YOUR SSO URL GOES HERE";
var settings = new Okta.Core.OktaSettings
{
ApiToken = apiToken,
BaseUri = new Uri(baseUrl)
};
var authClient = new Okta.Core.Clients.AuthClient(settings);
var authResponse = authClient.Authenticate(username, password);
var sessionToken = authResponse.SessionToken;
var sessionsClient = new Okta.Core.Clients.SessionsClient(settings);
var session = sessionsClient.CreateSession(sessionToken);
var cookieToken = session.CookieToken;
var httpClient = new System.Net.Http.HttpClient();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "UnitTest");
string url = string.Format("{0}?onetimetoken={1}", ssoUrl, cookieToken);
using (var response = await httpClient.GetAsync(url))
{
if (response.StatusCode == HttpStatusCode.OK)
{
string html = await response.Content.ReadAsStringAsync();
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(html);
string samlResponse = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='SAMLResponse']").Attributes["value"].Value;
return samlResponse;
}
else
throw new Exception(string.Format("Error getting SAML Response {0}", response.StatusCode));
}
}
catch (Exception ex)
{
throw;
}
}
source
share