2016-11-08 15 views
3

ADAL v3はUserPasswordCredentialクラスを持っているが、私は作業の実装を見つけることができません。 UserPasswordCredentialまたはUserCredentialタイプを受け入れるAcquireTokenオーバーロードはありません。 ADAL v3のユーザー名&のパスワードフローを実行する正しい方法は何ですか?この特定のコードは完全な.Net 4.5を使用しています。ADAL v3:UserPasswordCredentialを使用して認証する方法

答えて

3

あなたがクライアントアプリで開発していた場合は、トークンを取得するために以下のコードを参照することができます。

string authority = "https://login.microsoftonline.com/xxxx.onmicrosoft.com"; 
string resrouce = "https://graph.windows.net"; 
string clientId = ""; 
string userName = ""; 
string password = ""; 
UserPasswordCredential userPasswordCredential = new UserPasswordCredential(userName,password); 
AuthenticationContext authContext = new AuthenticationContext(authority); 
var token= authContext.AcquireTokenAsync(resrouce,clientId, userPasswordCredential).Result.AccessToken; 

そして、あなたは、Webアプリケーション(これは一般的なシナリオではありません)を開発していた場合には、そのようなはありませんこのシナリオをサポートするADAL V3のメソッド回避策として、あなた自身でリクエストを作成することができます。ここではあなたの参照のための例です:

POST: https://login.microsoftonline.com/xxxxx.onmicrosoft.com/oauth2/token 

Content-Type: application/x-www-form-urlencoded 
resource={resource}&client_id={clientId}&grant_type=password&username={userName}&password={password}&scope=openid&client_secret={clientSecret} 
+2

感謝。 docs.microsoft.comの新しいドキュメンテーションサイトは信じられないほど読みにくく、adal v3は使いにくいです。 – Trondh

+0

この場合、クライアント秘密はどこで使用されますか?私はそれが必要だと思う。 – Kurkula

+2

@Trondh DITTOは、私は、エラー、次のリクエストボディには、次のパラメータが含まれている必要があります取得し、このがらくた – Mikee

2

受け入れ答えの第二部について詳しく説明するために、ここでPOSTリクエストを作る実装です:

From SettingHelper: public static string GetAuthorityEndpoint(string azuretenantId) => $"https://login.microsoftonline.com/{azuretenantId}/"; 

    private static async Task<OAuthResult> AuthenticateAsync(string resource = "https://yourAzureADProtectedResource.url/") 
    { 
     var oauthEndpoint = new Uri(new Uri(SettingsHelper.GetAuthorityEndpoint("your AAD Tenent ID")), "oauth2/token"); 

     using (var client = new HttpClient()) 
     { 
      var result = await client.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[] 
      { 
       new KeyValuePair<string, string>("resource", resource), 
       new KeyValuePair<string, string>("client_id", "your AAD App Id"), 
       new KeyValuePair<string, string>("grant_type", "password"), 
       new KeyValuePair<string, string>("username", "[email protected]"), 
       new KeyValuePair<string, string>("password", "your password"), 
       new KeyValuePair<string, string>("scope", "openid"), 
       new KeyValuePair<string, string>("client_secret", "an access key for your AAD App"), 
      })); 

      var content = await result.Content.ReadAsStringAsync(); 
      var authResult = JsonConvert.DeserializeObject<OAuthResult>(content); 
      return authResult; 
     } 
    } 

    class OAuthResult 
    { 
     public string Token_Type { get; set; } 
     public string Scope { get; set; } 
     public int Expires_In { get; set; } 
     public int Ext_Expires_In { get; set; } 
     public int Expires_On { get; set; } 
     public int Not_Before { get; set; } 
     public Uri Resource { get; set; } 
     public string Access_Token { get; set; } 
    } 

あなたは、その後の使用に進むことができますこのような認証結果:

private async Task<HttpClient> GetHttpClientWithAzureADAuthentication() 
    { 
     OAuthResult authResult; 
     try 
     { 
      authResult = await AuthenticateAsync(); 
      var httpClient = GetHttpClient(); 
      httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {authResult.Access_Token}"); 

      return httpClient; 
     } 
     catch (Exception e) 
     { 
      Debug.WriteLine(e); 
      throw; 
     } 
    } 
+0

明らかにあなたは、ハードコードのものではないだろうここに、彼らは解決策を説明するためにここにあります:) –

関連する問題