I need to call an endpoint to perform and retrieve 3 cookies to call another application's endpoint, and I'm using flurl 3.0 to perform the call. The login is successful, but the cookie session is always empty. If I perform the call with Postman, I see the cookies and call the other endpoint. This is the code I'm using:
var loginData = new
{
lt = GetLoginTicket(),
username = _enConfig.Username,
password = _enConfig.Password
};
using var session = new CookieSession(loginUrl);
{
var login = await session.Request(loginUrl).AllowAnyHttpStatus().PostUrlEncodedAsync(loginData);
_jar = login.ResponseMessage.IsSuccessStatusCode ? session.Cookies : null;
}
The documentation of the application says this:
The response code can be:
302, a redirection The authentication is successful. An SSO cookie (also named ######) is sent in the response header.
I get the 302 Status Code and in the body, I get the correct HTML page, but the cookies are always empty.
Am I missing something?
EDIT: I've modified the code as follows to have the Cookies. I almost solved my problem. Still, 1 cookie is missing after the post with the Url encoded body. The code is now:
using var session = new CookieSession(_enConfig.PassportBaseEndpoint);
{
_logger.LogInformation("Getting login ticket");
var ltData = new LoginTicket();
var ltResponse = await session.Request("/login?action=get_auth_params").GetAsync();
if (ltResponse.ResponseMessage.IsSuccessStatusCode)
{
var ltString = await ltResponse.ResponseMessage.Content.ReadAsStringAsync();
ltData = JsonConvert.DeserializeObject<LoginTicket>(ltString);
if (ltData == null || string.IsNullOrEmpty(ltData.Lt))
throw new Exception("Could not deserialize login token");
}
else
throw new Exception("Could not get Login Ticket from enovia");
var loginData = new
{
lt = ltData.Lt,
username = _enConfig.Username,
password = _enConfig.Password
};
var login = await session.Request("/login/").AllowAnyHttpStatus().PostUrlEncodedAsync(loginData);
_jar = login.ResponseMessage.IsSuccessStatusCode ? session.Cookies : null;
}
After the first GET call to retrieve the login Ticket, I also get 2 cookies with a SessionID. When I perform the POST, I must also pass these cookies to have a successful login. In the response, I should get 1 more cookie with a token. The problem now is that the login is successful, but I don't get the third cookie in the session or the login response.
EDIT 2: Without Flurl I'm able to get the cookies with this code
CookieContainer cookies = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = cookies;
handler.ServerCertificateCustomValidationCallback = (a, b, c, d) => true;
var client = new HttpClient(handler);
var response = client.GetAsync($"{_enConfig.PassportBaseEndpoint}/login?action=get_auth_params").Result;
var loginTicket = JsonConvert.DeserializeObject<LoginTicket>(await response.Content.ReadAsStringAsync());
var loginBody = new Dictionary<string, string>();
loginBody.Add("lt", loginTicket.Lt);
loginBody.Add("username", _enConfig.Username);
loginBody.Add("password", _enConfig.Password);
var encodedContent = new FormUrlEncodedContent(loginBody);
response = await client.PostAsync($"{_enConfig.PassportBaseEndpoint}/login", encodedContent);
_logger.LogInformation(await response.Content.ReadAsStringAsync());
Uri uri = new Uri($"{_enConfig.PassportBaseEndpoint}/login");
IEnumerable<Cookie> responseCookies = cookies.GetCookies(uri).Cast<Cookie>();
foreach (Cookie cookie in responseCookies)
Console.WriteLine(cookie.Name + ": " + cookie.Value);