How to use PowerShell to open and receive response from authorization dialog of Azure Active Directory OAuth Authorization Code Grant Flow

Question:

I am trying to write a PowerShell script that let users to authorize an Azure Active Directory application to act on their behalf.

Following the Authorization Code Grant flow documentation from Microsoft. I am invoking the Authorization endpoint with the following line:

It simultaneously return a response in PowerShell and open a web page on my default browser at the same time.

Below is what the response looks like:

The web page displayed in the browser is redirected to https://login.microsoftonline.com/cookiesdisabled with the following message displayed:

We can’t sign you in
Your browser is currently set to block cookies. You need to allow cookies to use this service.
Cookies are small text files stored on your computer that tell us when you’re signed in. To learn how to allow cookies, check the online help in your web browser.

So to summarize, it does not work!

Note that, cookies is enabled in my browser, and if I invoke the web request from the web browser, it will work just fine.

What am I doing wrong? and how do I prompt the dialog to the user using PowerShell and receive the authorization code response back in the PowerShell?

Thank you.

Answer:

You’re right. This is becuasue that Poweshell cannot catch the authorization code that comes back with the Reply URL.

Solution:

You can write a login brower mudule to act as a “fake” endpoint for the authorization code that comes back with the Reply URL.

Try to use this Example :

LoginBrowser


REST with AUTHCODE

Source:

How to use PowerShell to open and receive response from authorization dialog of Azure Active Directory OAuth Authorization Code Grant Flow by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply