2016-08-17 205 views
1

我正在嘗試使用AAD對我的客戶端進行身份驗證,並使用Windows服務自動執行此操作。在AAD .NET SDK中,有兩種方法,AcquireTokenAsyncAcquireToken,但我不能使用這些方法之一,則調用的await將沒有任何反應相依相偎,當我做這樣的事情:使用用戶名和密碼獲取Azure Active Directory標記

result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result; 

該對象返回狀態Waiting for Activation & Code 31 ..

現在,是否有無法使用硬編碼的用戶名和密碼獲取令牌?

我全碼:

 string hardcodedUsername = "username"; 
     string hardcodedPassword = "password"; 

     string tenant = "[email protected]"; 
     string clientId = "clientId"; 
     string resourceHostUri = "https://management.azure.com/"; 
     string aadInstance = "https://login.microsoftonline.com/{0}"; 

     string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant); 


     authContext = new AuthenticationContext(authority); 

     AuthenticationResult result = null; 
      try 
      { 

       result = authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)).Result; 
      } 
      catch (Exception ex) 
      { 
       System.Diagnostics.Debug.WriteLine(ex.Message); 
      } 

      return result; 

我試圖以訪問Azure的API。

更新1:

我得到這個輸出,當我試圖await通話,我想這可能幫助:

Microsoft.IdentityModel.Clients.ActiveDirectory TokenCache:展望... Microsoft.IdentityModel.Clients.ActiveDirectory TokenCache:在緩存中找不到匹配的令牌 Microsoft.IdentityModel.Clients.ActiveDirectory d__0:將用戶領域發現請求發送到'https://login.microsoftonline.com/common/UserRealm/用戶名 API-版本= 1.0' Microsoft.IdentityModel.Clients.ActiveDirectory d__4:用戶與散列 '?***' 檢測爲 '聯合'

+0

您是否嘗試過在'authContext.AcquireTokenAsync()'方法調用之前創建方法'async'並將'await'放入? –

+0

是的,它需要永遠沒有迴應,我提到在我的問題 –

+0

你可以分享完整的代碼(包括方法簽名以及如何調用此方法)? –

回答

0

嘗試下面鏈路代碼

https://msdn.microsoft.com/en-in/library/partnercenter/dn974935.aspx

how to get access token after windows azure active directory authentication

How to get current token from Azure ActiveDirectory application

// Get OAuth token using client credentials 
string tenantName = "GraphDir1.OnMicrosoft.com"; 
string authString = "https://login.microsoftonline.com/" + tenantName; 

AuthenticationContext authenticationContext = new AuthenticationContext(authString, false); 

// Config for OAuth client credentials 
string clientId = "118473c2-7619-46e3-a8e4-6da8d5f56e12"; 
string key = "hOrJ0r0TZ4GQ3obp+vk3FZ7JBVP+TX353kNo6QwNq7Q="; 
ClientCredential clientCred = new ClientCredential(clientId, key); 
string resource = "https://graph.windows.net"; 
string token; 
try 
{ 
    AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientCred); 
    token = authenticationResult.AccessToken; 
} 
catch (AuthenticationException ex) 
{ 
    Console.ForegroundColor = ConsoleColor.Red; 
    Console.WriteLine("Acquiring a token failed with the following error: {0}", ex.Message); 
    if (ex.InnerException != null) 
    { 
     // You should implement retry and back-off logic according to 
     // http://msdn.microsoft.com/en-us/library/dn168916.aspx . This topic also 
           // explains the HTTP error status code in the InnerException message. 
     Console.WriteLine("Error detail: {0}", ex.InnerException.Message); 
    } 
} 
+0

2件事情:1)OP想要使用用戶名/密碼組合而不是使用客戶端憑證獲取憑證,以及2)OP使用異步方法。你已經顯示的是同步方法,你正在使用客戶端憑證。 –

+0

準確地說,因爲我在AAD的應用程序是Native Client,所以我無法獲得進行此類身份驗證的密鑰,我需要的是使用我的憑據(用戶名和密碼) –

0

請嘗試以下方法:

static void Main(string[] args) 
    { 
     Task<AuthenticationResult> t = getAccessToken(); 
     t.Wait(); 
     var result = t.Result; 
     Console.WriteLine(result.AccessToken); 
     Console.WriteLine("Please any key to terminate the program"); 
     Console.ReadKey(); 
    } 

    public static async Task<AuthenticationResult> getAccessToken() 
    { 
     string hardcodedUsername = "username"; 
     string hardcodedPassword = "password"; 

     string tenant = "tenant.onmicrosoft.com"; 
     string clientId = "clientId"; 
     string resourceHostUri = "https://management.azure.com/"; 
     string aadInstance = "https://login.microsoftonline.com/{0}"; 

     string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant); 


     var authContext = new AuthenticationContext(authority); 

     AuthenticationResult result = null; 
     try 
     { 
      result = await authContext.AcquireTokenAsync(resourceHostUri, clientId, new UserCredential(hardcodedUsername, hardcodedPassword)); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.StackTrace); 
      System.Diagnostics.Debug.WriteLine(ex.Message); 
     } 

     return result; 
    } 

我所做的是由getAccessToken()方法async和內部的代碼必須等待,當你調用authContext.AcquireTokenAsync獲得令牌。

+0

我之前嘗試過,等待沒有任何響應,但這次我檢查輸出,有一些有趣的..我編輯我的問題,所以你可以看到輸出.. –

+0

它看起來像用戶來自其他Azure廣告。您可以嘗試在創建應用程序的AD中使用用戶的憑據嗎? –

+0

是的,用戶在具有完全權限的目錄中! –

相關問題