問題描述
這個想法是讓它像 Azure Powershell 一樣工作,即彈出標準的 Active Directory 身份驗證對話框,我輸入我的憑據,AzureAD 完成它的工作,然后控制臺應用程序無法訪問某個 Web 服務.
The idea is to make it work like Azure Powershell, i.e. the standard Active Directory authentication dialog pops up, I enter my credentials, AzureAD does its thing and then the console application cann access a certain webservice.
這可能嗎?應該沒那么難,但我找不到例子.
Is this possible? It shouldn't be that hard but I could find no example.
查看 WPF 示例,我看到它在 app.config 中設置了一個客戶端 ID.這真的有必要嗎?我的意思是,單頁 js 應用也沒有向 AD 注冊,是嗎?
Looking at the WPF example, I see it sets a client id in the app.config. Is this really necessary? I mean, a single page js app isn't registered with the AD either, is it?
推薦答案
事實上,任何客戶端應用程序都應該在 AAD 中注冊.
In fact, any client application should be regsitered in AAD.
控制臺應用程序也必須在 Azure AD 中注冊,因此您將擁有客戶端 ID 和客戶端重定向 URL,但沒有客戶端密碼.那么下面的代碼應該可以工作了:
Console app must be registered in Azure AD too, so you'll have client id and client redirect url, but no client secret. Then the following code should work:
void Main()
{
var clientId = "client-GUID";
var clientUrl = new Uri("redirect-url"); //can be anything starting with localhost
var tenant = "name of your AAD tenant";
string authority = "https://login.windows.net/" + tenant;
string resource = "https://outlook.office365.com"; ///put id or url of resource you're accessing
AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
Console.WriteLine("Trying to acquire token");
var pp = new PlatformParameters(PromptBehavior.Auto); //this brings web prompt
var token = authenticationContext.AcquireTokenAsync(resource, clientId, clientUrl, pp, UserIdentifier.AnyUser).Result;
Console.WriteLine("Got the token: {0}", token.AccessToken);
}
這篇關于是否可以將 AzureAD 身份驗證與控制臺應用程序一起使用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!