Azureサブスクリプションには、「必要なアクセス許可」セクションで設定したグラフへのアクセスが必要なアプリ登録があります。有効なJWTを使用したグラフ認証
当社角度アプリケーションは素晴らしい....、AADに対する認証をキックオフJWTを受け、ヘッダのすべての要求にそれを追加し、それが正常に検証されています私たちのWEBAPIにそれを送信しています!
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjlGWERw...
は今、WEBAPIから、私はまた、グラフ化するための要求を作りたいと私はAuthorizationヘッダを経由して、グラフのREST APIに渡すんだ、検証済みのトークンを持って考えます。
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
しかし、401(Unauthorized)の結果が表示されています。どうして?明らかに私はパズルの一部を欠いているが、私は何がわからない。
Azureアプリの登録でグラフパーミッションを設定すると、Angularアプリにトークンを送信したときにAADとGraphの両方で有効であると思われましたが、そうではないようです。
私のJWTは次のようになります。 「AUD」を注意のみ(それが問題だ場合、私はそれをどのように修正するのですか?)私のアプリの登録IDとないグラフを示し
{
"aud": "<application id here>",
"iss": "https://sts.windows.net/<tenant id here>/",
"iat": 1499121655,
"nbf": 1499121655,
"exp": 1499125555,
"aio": "ASQA2/8DAAAACuKzzzni2uaVoaIb9yJa4j3XuG0O+9cQQQlnqXl8Sr0=",
"amr": [
"pwd",
"mfa"
],
"family_name": "Smith",
"given_name": "John",
"name": "John Smith",
"nonce": "<nonce here>",
"platf": "3",
"pwd_exp": "941678",
"sub": "<sub here>",
"tid": "<tenant id here>",
"unique_name": "[email protected]",
"upn": "[email protected]",
"ver": "1.0"
}
EDIT私は今オンbehalf-を実装しました
のコードはNan Yuと書かれていますが、私はまだ401を取得しています。フロントエンドのトークンはWebAPIで検証されており、WebAPIはGraphへのリクエストを作成しています。
string authString = ConfigurationManager.AppSettings["ida:Authority"];
string clientId = ConfigurationManager.AppSettings["ida:Audience"];
string clientSecret = ConfigurationManager.AppSettings["ida:AppSecret"];
string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
string tenant = ConfigurationManager.AppSettings["ida:Tenant"];
string graphResourceId = ConfigurationManager.AppSettings["ida:GraphResourceId"];
AuthenticationContext authenticationContext = new AuthenticationContext(authString);
ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
var bootstrapContext = ClaimsPrincipal.Current.Identities.First().BootstrapContext as System.IdentityModel.Tokens.BootstrapContext;
string userName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn) != null ? ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value : ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;
string userAccessToken = bootstrapContext.Token;
UserAssertion userAssertion = new UserAssertion(bootstrapContext.Token, "urn:ietf:params:oauth:grant-type:jwt-bearer", userName);
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationResult result = await authenticationContext.AcquireTokenAsync(graphResourceId, clientCred, userAssertion);
これは、現在返されているため、トークンの正常に動作しますが、グラフの「AUD」としてログインしたユーザーのための「SCP」があります。コールしようとしたとき
"aud": "https://graph.windows.net",
"scp": "User.Read User.ReadBasic.All",
"unique_name": "[email protected]",
"upn": "[email protected]",
をなぜ私はまだ不正です/meグラフAPI?
ご協力いただきありがとうございます。これは間違いなく私を動かすが、私はまだ401を手に入れている。 –
2つのオプションの重要な違いの1つが示されています。前者はユーザー認証で、ユーザーに代わってリソースにアクセスするスコープを与えます。これは実際の代理フローだけでなく、認証コードと暗黙のフローにも当てはまります。後者のオプション(クライアント認証情報)はアプリ認証です。この場合、Azure ADは、あなたのアプリケーション自身がアクセスできる役割を持つトークンをあなたのアプリケーションに付与しますが、あなただけのアクセス権はアプリケーションに与えません。 @ GT-Down FYIより。 「設定:詳細は、チェックアウト[本](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-integrating-applications)docに関するヘッダを検索 –
Web APIを呼び出すためのクライアントアプリケーション "を参照してください。 –