Login Web with Invoke-WebRequest not working

75 Views Asked by At

I'm trying to automate a login into a webpage via invokewebrequest.

 $urlLogin = "https://XXXXXX.es/web/XXXXX/inicio?p_p_id=com_liferay_login_web_portlet_LoginPortlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&_com_liferay_login_web_portlet_LoginPortlet_javax.portlet.action=%2Flogin%2Flogin&_com_liferay_login_web_portlet_LoginPortlet_mvcRenderCommandName=%2Flogin%2Flogin"

$loginPayload = @{
    _com_liferay_login_web_portlet_LoginPortlet_formDate                = "1692051569185"
    _com_liferay_login_web_portlet_LoginPortlet_saveLastPath            = $False
    _com_liferay_login_web_portlet_LoginPortlet_redirect                = ""
    _com_liferay_login_web_portlet_LoginPortlet_doActionAfterLogin      = $False
    _com_liferay_login_web_portlet_LoginPortlet_conexionInterna         = ""
    _com_liferay_login_web_portlet_LoginPortlet_masinformacionservicios = ""
    _com_liferay_login_web_portlet_LoginPortlet_login                   = $secrets.userbida
    _com_liferay_login_web_portlet_LoginPortlet_password                = $secrets.pwdbida
    p_auth                                                              = ""
}
$responseLogin = Invoke-WebRequest -Uri $urlLogin -Body $loginPayload -Method "POST" -SessionVariable session -MaximumRedirection 0 -SkipHttpErrorCheck -ErrorAction SilentlyContinue
if ($responseLogin.Headers.'Set-Cookie' -like '*JSESSIONID*') {
    Write-Host (get-date -format "dd-MM-yyyy HH:mm:ss") "INFO: Log In Succeded: $($responseLogin.Headers.'Set-Cookie'.Count) Cookies obtained`n$($responseLogin.Headers.'Set-Cookie')"
}
else {
    Write-Host (get-date -format "dd-MM-yyyy HH:mm:ss") "ERROR: Function $($MyInvocation.MyCommand) - Unable to get Cookies. Login Failed. Check parameters file or web status"
    throw
}

Response is 302 and I obtain 3 cookies:

$responseLogin.Headers.'Set-Cookie'
JSESSIONID=B39C532969F757DA32595072FE9C48E8.srvbidp089; Path=/; Secure; HttpOnly
COOKIE_SUPPORT=true; Max-Age=31536000; Expires=Wed, 14-Aug-2024 14:04:06 GMT; Path=/; Secure; HttpOnly
GUEST_LANGUAGE_ID=es_ES; Max-Age=31536000; Expires=Wed, 14-Aug-2024 14:04:06 GMT; Path=/; Secure; HttpOnly

But when I use the Session variable for a request, it always responds home html as If i'm not logged in.

EDIT

This is the process I do via code in order to try the login. Second request is the one

  1. Post Login data to URL https://XXXX.es/web/XXXX/inicio?p_p_id=com_liferay_login_web_portlet_LoginPortlet&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&_com_liferay_login_web_portlet_LoginPortlet_javax.portlet.action=%2Flogin%2Flogin&_com_liferay_login_web_portlet_LoginPortlet_mvcRenderCommandName=%2Flogin%2Flogin And I avoid the redirect to obtain the cookies. Request in Developer Tools
  2. I use the session to get my user homepage doint a GET to https://XXXX.es/group/XXX/inicio. With this I should get the HTML file of my homepage, but instead it gives me status code 200 and the default homepage of the web, as if I was not logged in:https://xxxx.es/web/xxxx Request for user HomePagee

This script has worked before in other webpages, but for some reason this one is different.

Additional Information:

If I do the steps on the navigator, login request headers returns same code "302" but with 10 additional cookies, so I guess the issue must be this.

I've tried to add this cookies manualy downloading them in JSON format and then adding to the session variable. But it did not work....

Any help is appreciated!

BR

0

There are 0 best solutions below