Microsoft Graph-授权流程-从控制台应用程序获取授权代码 [英] Microsoft Graph - Authorization Flow - Get Authorization Code from Console App

查看:56
本文介绍了Microsoft Graph-授权流程-从控制台应用程序获取授权代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure AD注册的应用程序和一个AD用户.我想使用我的c#控制台应用程序中的授权流来首先获取本文 https://docs.microsoft.com/zh-cn/graph/auth-v2-user ,然后交换该身份验证代码以获取访问令牌.我最终希望存储访问令牌和刷新令牌,并将其用于离线方案.

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.

我创建了一个Httpclient并发出了发布请求以获取授权代码,但没有弹出"Microsoft同意"屏幕.

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?

推荐答案

HttpClient只会从login.microsoftonline.com返回用于登录的HTML,但无处可显示.HttpClient可以执行GET,POST等操作,但不是Web视图.

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.

我从未测试过从控制台应用程序扩展Web视图的情况,因此我不确定是否可行.可以通过PowerShell完成此操作,但这与控制台应用程序不同.您可以检查一下.

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

这篇关于Microsoft Graph-授权流程-从控制台应用程序获取授权代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆