简体   繁体   中英

Microsoft Graph - Authorization Flow - Get Authorization Code from Console App

I have an Azure AD registered App and a AD user. I would like to use authorization flow in my c# console app to first get the authorization code as mentioned in this article https://docs.microsoft.com/en-us/graph/auth-v2-user and then exchange that auth code to get an access token. I would eventually like to store the access token and refresh token and use it for offline scenarios.

I created a Httpclient and did a post request to get the authorization code but the Microsoft Consent screen does not pop up.

Is this even possible from a console app, or do I have to have a web app to do this? Any samples or articles that can help me?

A HttpClient will only return the HTML for sign-in from login.microsoftonline.com, but will have nowhere to show it. The HttpClient can do GET, POST etc but is not a web view.

I have never tested spinning up a webview from a Console app, so I'm not sure if it's possible. It can be done from PowerShell, but that is not the same as a Console app. You may check that out.

Or you can let the user enter the username and password in the Console app and use the "password" grant type flow to obtain the access token. it's not recommend, but it's possible. You can read more about the password grant type flow here:

https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM