推奨されるアプローチは、AuthorizationCodeReceived
イベントを使用して認証コードをアクセス トークンに交換することです。 Vittorio は、全体の流れを概説するブログ エントリを持っています。
これをセットアップするためのコードのGitHub 上のサンプル アプリの例を次に示します。Startup.Auth.cs
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
AuthenticationContext authContext = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenantID), new EFADALTokenCache(signedInUserID));
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceID);
return Task.FromResult(0);
},
...
}
注:このAuthorizationCodeReceived
イベントは、承認が実際に行われるときに 1 回だけ呼び出されます。認証コードがすでに生成されて保存されている場合、このイベントは呼び出されません。このイベントを強制的に実行するには、ログアウトするか Cookie をクリアする必要があります。