r/PowerShell • u/Jwtfeldman • Jun 07 '26
Question Powershell to log into a SSO website
I am hoping someone can assist me. Im attempting to sign into a website my company hosts through powershells invoke-webrequest. My issue is we use Azure for SSO. The final invoke-webrequest i need to make requires a SAMLResponse value. I have searched through all the network logs of the dev tools in chrome but none hold this value.
Does anyone have any experience with this?
30
Upvotes
21
u/AuditMind Jun 07 '26
You are probably looking at the wrong layer.
SAMLResponse is created by the IdP and browser-posted to the app's ACS endpoint. It is not something Invoke-WebRequest can simply fetch from the final page.
For production automation, use a supported API/OAuth/MSAL flow. For UI testing, use Playwright or Selenium and let the browser handle SSO.
7
u/screamtracker Jun 07 '26
It sounds like the wrong approach. Google 'oauth 2.0 device code flow with powershell' and get your own token for your session
3
u/Nu11u5 Jun 07 '26 edited Jun 07 '26
I would display a WebBrowser control, initiate the SAML login flow, wait for the browser object to redirect back to the SP, and extract the session cookie. Then, export the cookie for reuse and inject it into a WebRequestSession object and call Invoke-WebRequest with it.
You can rerun the authentication routine as needed.
If the IDP or SP don't like that the client is "IE", then you need to use WebView2 and this solution gets more complicated.
7
u/raip Jun 07 '26
You have two options without engaging IT -
2
u/greenSacrifice Jun 07 '26
You can find the value using something like wireshark but don’t run that one your company network. And when you do you’ll likely find the app registration is setup so the reply url doesn’t send your localhost the data. Best to just save the cookie being used.
3
u/Flysquid18 Jun 07 '26
I see two things here. Both are similar to a scenario I am going through. What you NEED is an API endpoint that does what you want. The justification? If I read correctly, you need a process/solution to work indefinitely. If it IS that important, then an API endpoint needs to be developed.
My situation was proving that our site needed an API. I wrote a powershell module that leverages Selenium to perform Azure SSO authentication. Once completed, I stored the cookies and made direct Webrequest calls. It took 4 years for people to recognize the importance and need for an API.
Back to you, in my opinion, if you are writing something "needed" in PowerShell, then it's a proof of concept that needs to be adopted as a feature/service of your platform. Talk to the right people. Lobby for the request. Identify the cost savings.
2
u/theveganite Jun 07 '26
The absolute best approach is to stop trying to scrape the web GUI entirely. If the application has a proper REST API, you should create an app registration in Azure AD, assign the necessary Microsoft Graph or custom API permissions, and use the OAuth 2.0 client credentials flow to pull a bearer token.
If the application has absolutely no API and strictly requires web scraping, you need to abandon Invoke-WebRequest and use a browser automation framework like Playwright or Selenium. Those tools can actually render the JavaScript, handle the redirects natively, and pass through the SSO flow like a real user.
Your current approach is a ticking time-bomb. It will break regularly. Not sustainable.
2
u/g3n3 Jun 07 '26
So your initial call to mycompanyapp.mydomain.net will return the html with embedded js that you need to pull out to derive the post for the login. Then the xml posting back and forth. MFA. And then you are in. Then you have replicate the calls made by your company app itself to get the data.
2
u/jeek_ Jun 07 '26
What about something like this, https://github.com/alflokken/PSAuthClient
1
u/alanjmcf Jun 07 '26
If I understand, you’re trying to grab a security value from browser tools to use in your code. What not just do the real authentication in your code?
I’ve implemented OAuth authentication in my PowerShell app. (Hope I’m getting the terminology correct!) In my case I’m doing Certificate-Based Auth using self-signed certificates to connect to Defender API. I create an App Registration in Entra and upload the certificate to it. In my case that’s better than authenticating as a user. If hosting in Azure the current option I read, is managed identities.
I’m using example code from a blog to form the authentication blob to pass to the web request. The other option is to use the Microsoft Authentication Libraries, but I was trying to reduce dependencies — and Microsoft’s 365 modules’ authentication conflict hell.
1
u/Automatic-Let8857 Jun 07 '26
1
u/imatt3690 Jun 07 '26
Powershell SSO via SAML using the logon user account is a terrible practice. If you are using Azure. Setup an app registration to the site you are Logging into (redirect). Next setup API permissions via graph scoped to the user+token credentials you need. You’ll then generate a client ID and secret for that application registration. Next you’ll use your company secrets engine to store those credentials (Hashicorp Vault / Cyberark / whatever) and you’ll need to create a credential flow to fetch those out of your company vault so you’re not physically hard coding credentials into the script or application you’re writing. Using those credentials you’ll now perform an authorization token request from azure to get your graph token. Then use that token to authenticate to whatever service / site you want via API or preferred interface.
If your requirements are to Powershell SSO into a browser on a computer that can’t have a browser engine, you need to reevaluate life choices lol
1
u/LALLANAAAAAA Jun 07 '26
OP a lot of these replies are just confusing the issue so let's back up a step. Do you know what the consumer URL is? If not, then you need to find it, because that's where the authenticated agent (browser & session) is sending the SAML.
In the real deal (browser) auth flow, the SAML response should be base64 encoded and appear to be a POST request payload from your browser to the service's consumer URL.
After initial auth, the identify provider is telling your browser to deliver the signed SAML assertion to the consumer, so the consumer can validate the assertion and allow you to access the actual service or whatever.
If you know the consumer URL, look for the redirect that triggers the POST request to that URL in the dev tools, if your auth was successful, the signed SAML assertion will be there.
You don't actually need to do any cookie stealing or whatever if powershell is the agent with the session and the cookie, but it still may be useful to look at the real browser network requests to understand the order of operations.
Anyway, step 1 is to find the consumer URL. If you are successfully logging in with anything, the SAML is being sent from your browser to the consumer in a post request.
1
u/g3n3 Jun 07 '26
Yes I have done this. You make the initial call to pass your creds. Then you get back a few xml pieces and to manually exchange it and make the calls yourself. Then maybe MFA. I did it with Duo too. Possible, you just have to parse xml and maybe html and make several calls. The dev tools kind of hide it I believe. You would make the calls yourself and check on the xml output. Each call gives you the next call until you get through the SSO.
1
u/g3n3 Jun 07 '26
You also have to parse the JS ast to get the config to be able to pass the canary, ctx, sessionid, etc. these live in a JavaScript code variable inside html. So you have to parse html and get the JavaScript and then parse the JavaScript into ast to get the values. The urlpost variable will be the login uri. Once you post on the login uri, then you have to do your xml rest method posting to the redirect and maybe some MFA.
1
u/g3n3 Jun 07 '26
Psparsehtml module is going to be a requirement.
1
u/g3n3 Jun 07 '26
In all these tasks, you just have to recreate the JavaScript code to build all the calls or you have playwright or selenium engines running the JavaScript client side for you. I tend to want to recreate the JavaScript where it is fairly simple and make the calls directly like I have been explaining.
1
u/g3n3 Jun 07 '26
It has taken me years and years to get to the point of reading dev tools and parsing and such and reverse engineering the browser. I can pretty much do any web site without serious captchas.
1
u/g3n3 Jun 10 '26
What did you end up doing?
1
u/MadBoyEvo Jun 17 '26
Please check https://github.com/EvotecIT/HtmlTinkerX and more directly PSParseHTML it has whole browsing experience builtin ready for automation.