您可以在不使用ADAL的情況下發送管理員同意reuquest。由於您使用的WinForm的應用程序,我們可以使用WebBrowser控件來顯示登錄頁面,設置prompt=admin_consent
爲請求的URL。如果管理員同意,成功的一部分,檢查響應的admin_consent
值是否True
。下面的代碼是供你參考:
https://login.microsoftonline.com/<tenant>/oauth2/authorize?client_id=<client id>&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2F&nonce=1234&resource=https%3A%2F%2Fgraph.windows.net%2F&prompt=admin_consent
:
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var parameters = new Dictionary<string, string>
{
{ "response_type", "code" },
{ "client_id", "<client id>" },
{ "redirect_uri", "http://localhost" },
{ "prompt", "admin_consent"}
};
var requestUrl = string.Format("{0}/authorize?{1}", "https://login.microsoftonline.com/common/oauth2", BuildQueryString(parameters));
webBrowser1.Navigate(requestUrl);
}
protected string EndPointUrl
{
get
{
return string.Format("{0}/{1}", "https://login.microsoftonline.com/common", "");
}
}
private string BuildQueryString(IDictionary<string, string> parameters)
{
var list = new List<string>();
foreach (var parameter in parameters)
{
list.Add(string.Format("{0}={1}", parameter.Key, Uri.EscapeDataString(parameter.Value)));
}
return string.Join("&", list);
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if (e.Url.AbsoluteUri.StartsWith("http://localhost"))
{
var collection = System.Web.HttpUtility.ParseQueryString(e.Url.Query);
//check whether admin consent success
if (collection["admin_consent"] == "True")
{
//hide/close webBrowser
}
}
}
如果你不想做任何代碼,租戶管理員可以手動打開瀏覽器,並前往以下網址做管理員同意
謝謝。兩者似乎都很好。我不知道我會選擇哪種解決方案,但是這個問題已經解決了。 – Patsuan