私は同じ紺碧のアクティブディレクトリを使用する2つのアプリケーションを持っています。アプリAとアプリBの他のリソースへのアクセストークンを取得するためにAzure ADでクッキーを使用する方法
アプリAは
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
ClientId = Configuration["Authentication:AzureAd:ClientId"],
Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"],
ClientSecret = Configuration["Authentication:AzureAd:ClientSecret"],
CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],
ResponseType = OpenIdConnectResponseType.CodeIdToken,
GetClaimsFromUserInfoEndpoint = true,
SignInScheme = "Cookies",
SaveTokens = true,
Events = new OpenIdConnectEvents
{
OnAuthorizationCodeReceived = OnAuthorizationCodeReceived,
}
});
を使用していますし、私はとトークンを取得することにより、アプリケーションBのAPIサービスのリソースへのアクセスを取得:私もクッキーを使用してい
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
{
string userObjectId = (context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;
ClientCredential clientCred = new ClientCredential(Configuration["Authentication:AzureAd:ClientId"], Configuration["Authentication:AzureAd:ClientSecret"]);
AuthenticationContext authContext = new AuthenticationContext(Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"]);
AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
context.ProtocolMessage.Code, new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]), clientCred, Configuration["Authentication:AzureAd:GraphResourceId"]);
アプリAにログインする:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "Cookies",
AutomaticAuthenticate = true,
AutomaticChallenge = true,
SlidingExpiration = true,
ExpireTimeSpan = TimeSpan.FromHours(1),
Events = new CookieAuthenticationEvents()
{
OnSignedIn = OnSignedIn,
OnSigningIn = OnSigningIn,
OnValidatePrincipal = OnValidatePrincipal
}
});
/* Account Controller SignIn() */
return Challenge(
new AuthenticationProperties {
AllowRefresh = true,
IsPersistent = true,
RedirectUri = "/" }, OpenIdConnectDefaults.AuthenticationScheme);
私の問題は他のアクセストークン私のサインインのcookieがまだ有効なので、ユーザーはキャッシュにトークンを持っていませんが、正常にログインしているようです。
私は他の質問のスーツに続き、ここでの問題は、私は戻ってアダルAuthenticationContextが使用するデフォルトTokenStoreにこのトークンを取得する方法がわからないです
Task OnValidatePrincipal(CookieValidatePrincipalContext arg) {
var http = new HttpClient();
var uri = "https://login.microsoftonline.com/<tenant>/oauth2/token";
var client_id = "<my_client_id>";
var scope = "https://graph.microsoft.com/mail.read";
var refresh_token = "<saved_refresh_token_in_cookie_if_SaveTokens = true>";
var redirect_uri = "https://localhost:20352/";
var grant_type = "refresh_token";
var client_secret = "<client_secret_from_azure>";
var body = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("client_id", client_id),
new KeyValuePair<string, string>("scope", scope),
new KeyValuePair<string, string>("refresh_token", refresh_token),
new KeyValuePair<string, string>("redirect_uri", redirect_uri),
new KeyValuePair<string, string>("grant_type", grant_type),
new KeyValuePair<string, string>("client_secret", client_secret)
};
var content = new FormUrlEncodedContent(body);
var result = http.PostAsync(uri, content).Result;
var stringContent = result.Content.ReadAsStringAsync().Result;
JObject jobject = JObject.Parse(stringContent);
var token = jobject["access_token"].Value<string>();
の私のクッキーイベントに見てきました。代わって」トークンリフレッシュ/有効なトークンなしで、
_authenticationResult = await authContext.AcquireTokenSilentAsync(_authConfigOptions.AzureAd.WebserviceAppIdUri.ToString(), credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
私はトークン戻って、ユーザーのApp B API呼び出しのためのtokenstoreに新しいリソースへのアクセスを得ることができる方法があります:私たちは、からプルする必要が深いコードを持っていますユーザーのフロー?
アクセストークンまたはリフレッシュトークンがない場合、いいえ。 Azure ADで認証するためにそれらを送信する必要があります。しかし、キャッシュにリフレッシュトークンを入れてはいけませんか?最後の14日間(IIRC)ので、リダイレクトは必要ありません。 – juunas
まあ、アプリのCookieが認証されるシナリオを処理しようとしていますが、何らかの理由でリソースBのアクセス/リフレッシュトークンが無効であるかパージされています。再認証のためにリダイレクトすると、クッキーから引き出され、アクセストークンなしでアプリAに署名するパススルーが許可されます。私はその後、クッキーをパージし、これを処理するためにリダイレクトする必要がありますか? – cjsmith