プレビュー 2.0 エンドポイントを使用する MVC アプリがあります。プロフィール画像がデフォルトのオブジェクトではないことに少しがっかりしています。そうは言っても、エンドポイントを使用してプロファイル画像を適切に取得する方法を見つけようとして問題が発生しています。
何か案は?
プレビュー 2.0 エンドポイントを使用する MVC アプリがあります。プロフィール画像がデフォルトのオブジェクトではないことに少しがっかりしています。そうは言っても、エンドポイントを使用してプロファイル画像を適切に取得する方法を見つけようとして問題が発生しています。
何か案は?
ご指摘のとおり、Azure AD の OpenId connect は現在、ユーザーのプロフィール写真の取得をサポートしていません。
ただし、Azure AD アカウントのみで作業している場合は、Microsoft Graph を使用してユーザーのプロファイル画像を個別に取得できます。この REST を呼び出すには、User.Read スコープをアプリに付与します。参照用のコードは次のとおりです。
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
// The `Authority` represents the v2.0 endpoint - https://login.microsoftonline.com/common/v2.0
// The `Scope` describes the initial permissions that your app will need. See https://azure.microsoft.com/documentation/articles/active-directory-v2-scopes/
ClientId = clientId,
Authority = String.Format(CultureInfo.InvariantCulture, aadInstance, "common", "/v2.0"),
RedirectUri = redirectUri,
Scope = "openid email profile offline_access Mail.Read User.Read",
PostLogoutRedirectUri = redirectUri,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
// In a real application you would use IssuerValidator for additional checks, like making sure the user's organization has signed up for your app.
// IssuerValidator = (issuer, token, tvp) =>
// {
// //if(MyCustomTenantValidation(issuer))
// return issuer;
// //else
// // throw new SecurityTokenInvalidIssuerException("Invalid issuer");
// },
},
Notifications = new OpenIdConnectAuthenticationNotifications
{
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
AuthorizationCodeReceived = async (context) =>
{
var code = context.Code;
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
ConfidentialClientApplication cca = new ConfidentialClientApplication(clientId, redirectUri,
new ClientCredential(appKey),
new MSALSessionCache(signedInUserID, context.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase));
string[] scopes = { "Mail.Read User.Read" };
try
{
AuthenticationResult result = await cca.AcquireTokenByAuthorizationCodeAsync(scopes, code);
}
catch (Exception eee)
{
}
},
AuthenticationFailed = (notification) =>
{
notification.HandleResponse();
notification.Response.Redirect("/Error?message=" + notification.Exception.Message);
return Task.FromResult(0);
}
}
});
次に、以下の要求のような Azure AD ユーザーのプロファイル画像を取得できます。
Get: https://graph.microsoft.com/v1.0/me/photo/$value
authorization: bearer {token}
また、テストによると、Microsoft Graph は現在、Microsoft アカウントのプロファイル画像の取得をサポートしていません。Microsoft アカウントのサポートも必要な場合は、ここからフィードバックを送信できます。