问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/61001273/microsoft-graph-authorization-flow-get-authorization-code-from-console-app