2017-05-14 35 views
1

想法是使它像Azure Powershell一樣工作,即彈出標準Active Directory認證對話框,輸入我的憑據,AzureAD完成它的任務,然後控制檯應用程序不能訪問某個Web服務。是否可以在控制檯應用程序中使用AzureAD身份驗證?

這可能嗎? 它不應該那麼難,但我找不到任何例子。

看看WPF示例,我看到它在app.config中設置了一個客戶端ID。這真的有必要嗎?我的意思是,單頁js應用程序沒有在AD中註冊,是嗎?

+0

單個頁面的js應用程序將需要在AD中註冊才能使用AD認證 – Justin

+1

https://github.com/Azure-Samples/active-directory-dotnet-graphapi-安慰 ? – 4c74356b41

+0

控制檯應用程序訪問受Azure AD保護的Web API的示例 - https://blogs.msdn.microsoft.com/benjaminperkins/2016/10/20/how-i-connected-a-console-application-to-一個幅材-API保護的通過-AN-天青活性目錄/ – alwayslearning

回答

0

實際上,任何客戶端應用程序都應該在AAD中進行註冊。

控制檯應用程序也必須在Azure AD中註冊,因此您將擁有客戶端ID和客戶端重定向url,但沒有客戶端祕密。然後將下面的代碼應工作:

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); 
} 
相關問題