2016-08-17 8 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へのアクセスを取得しようとしています。

UPDATE 1:

Microsoft.IdentityModel.Clients.ActiveDirectory TokenCacheを::

私はawaitに電話をしようとしたとき、私は出力にこれを得た、私はこれが役立つかもしれないと思う探し... Microsoft.IdentityModel.Clients.ActiveDirectory TokenCache:キャッシュに一致するトークンが見つかりませんでした。 Microsoft.IdentityModel.Clients.ActiveDirectory d__0:ユーザー領域の探索要求を ''に送信しています APIバージョン= 1.0' Microsoft.IdentityModel.Clients.ActiveDirectoryのd__4:ハッシュを持つユーザー '?***リンクコード下の連合 '

+0

'async'メソッドを作成し、' authContext.AcquireTokenAsync() 'メソッド呼び出しの前に' 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が非同期メソッドを使用しています。あなたが示したのは、syncメソッドです。クライアントの資格情報を使用しています。 –

+0

正確には、AADのアプリがネイティブクライアントであるため、この種の認証を行うためのキーを取得できません。自分の認証情報(ユーザー名とパスワード)を使用する必要があります。 –

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からのようです。あなたは、アプリケーションを作成したADのユーザーの資格情報を使って試すことができますか? –

+0

はい、ユーザーは完全な権限を持つディレクトリにあります! –

関連する問題