私は、Microsoftのディレクトリからユーザーに関するいくつかの基本的なプロパティを取得する必要がある単純なデスクトップアプリケーションを作成しています。具体的にはAzure Active Directoryでの単純なディレクトリ参照
- 私は1つのテナントネイティブLOBアプリケーションを作成しています。
- アプリケーションはデスクトップ上で動作します。
- アプリケーションは、ログオンしたドメインアカウントとして実行されます。
- 組織のドメインアカウントは、AADに同期されます。
- 私は、ネイティブのWebアプリケーションやWeb APIなどを保護しようとしていません。ユーザーにサインインする必要はありません。
- 組織内の外部のイベント管理ツールから電子メールアドレスを取得しています。私は電子メールアドレスに基づいてAADからAADアカウントプロファイルデータ(アドレス帳情報、具体的には職種)を検索する必要があります。私はAADデータだけを読んでいます。
はこれまでのところ、私は次のように行われている: -
AzureのADグラフAPIは、プロファイル情報を取得するための正しい方法であることが表示されます。特に、この情報はエンドポイントで利用可能です:https://graph.windows.net/ {テナント}/users/{email}?api-version = 1.6
AADにネイティブアプリケーションを登録するとき、キーは提供されませんでした。だから、私はクライアントの秘密を持っていない。
ここのGitHubのサンプルはhttps://github.com/Azure-Samples/active-directory-dotnet-graphapi-consoleです。 Keysセクションが利用できないので、ここの手順は間違っているようです([(2)参照])。
上記のサンプルに基づいて、私は簡単な関数を書いた。以下のコードは次のとおりです。
private static async Task PrintAADUserData(string email)
{
string clientId = "0a202b2c-6220-438d-9501-036d4e05037f";
Uri redirectUri = new Uri("http://localhost:4000");
string resource = "https://graph.windows.net/{tenant}";
string authority = "https://login.microsoftonline.com/{tenant}/oauth2/authorize";
AuthenticationContext authContext = new AuthenticationContext(authority);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resource, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));
string api = String.Format("https://graph.windows.net/{tenant}/users/{0}?api-version=1.6", email);
LOG.DebugFormat("Using API URL {0}", api);
// Create an HTTP client and add the token to the Authorization header
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authResult.AccessTokenType, authResult.AccessToken);
HttpResponseMessage response = await httpClient.GetAsync(api);
string data = await response.Content.ReadAsStringAsync();
LOG.Debug(data);
}
質問
実行が認証ページを立ち上げることができたアプリケーション。なぜ私はそれが必要なのですか?アプリケーションはすでに自分のドメインアカウントとして実行されています。追加の認証は必要ですか? Azureでこのアプリケーションをワーカープロセスとして実行する場合、私は自分のドメイン資格情報を使用したくありません。
主な問題は、リソースURLが間違っているようです。 Azure AD Graph APIにアクセスするには、どのようなリソースを指定する必要がありますか?
おかげで、
Vijai。
EDITS
@Sacaからのコメントに基づいて、コードおよびアプリケーションが編集されています。
コード
string clientId = ConfigurationManager.AppSettings["AADClientId"];
string clientSecret = ConfigurationManager.AppSettings["AADClientSecret"];
string appIdUri = ConfigurationManager.AppSettings["AADAppIdURI"];
string authEndpoint = ConfigurationManager.AppSettings["AADGraphAuthority"];
string graphEndpoint = ConfigurationManager.AppSettings["AADGraphEndpoint"];
AuthenticationContext authContext = new AuthenticationContext(authEndpoint, false);
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.windows.net", new ClientCredential(clientId, clientSecret));
ExistingTokenWrapper wrapper = new ExistingTokenWrapper(authResult.AccessToken);
ActiveDirectoryClient client = new ActiveDirectoryClient(new Uri(graphEndpoint), async() => await wrapper.GetToken());
IUser user = client.Users.Where(_ => _.UserPrincipalName.Equals(email.ToLowerInvariant())).Take(1).ExecuteSingleAsync().Result;
のApp エラー 未処理の例外:System.AggregateException:1つの以上のエラーが発生しました。 ---> System.AggregateException:1つ以上のエラーが発生しました。 ---> Microsoft.Data.OData.ODataErrorException:操作を完了するための権限が不十分です。 ---> System.Data.Services.Client.DataServiceQueryException:この要求の処理中にエラーが発生しました。 System.Data.Services.Client.DataServiceClientException:{"odata.error":{"code": "Authorization_RequestDenied"、 "message":{"lang": "en"、 "value": "権限が不十分です"}}}
正しい権限を与えても正しいリソースとトークンを取得できるにもかかわらず、まだ何か不足しているようです。
から
は、あなたが得た例外を解決する方法を見つけたことがありますか?私はあまりにも正確な例外を取得するため** Authorization_RequestDenied ** –
@Arunkumarはい。 1月21日に追加した回答をご覧ください。テナントの管理者にAADの有無を確認してください。 – VJK