我有兩個Azure活動目錄應用程序設置。Azure活動目錄單租戶單獨的webapi應用程序訪問
爲簡潔起見,App-A
和App-B
。
A
是一個web應用程序,在本地主機上運行,而B
發佈到somedomain.azurewebsites.net
並公開webapi。
兩者都設置有:
app.UseCookieAuthentication(new CookieAuthenticationOptions(){
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
ClientId = Configuration["Authentication:AzureAd:ClientId"],
Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"],
CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],
TokenValidationParameters = new TokenValidationParameters{
SaveSigninToken = true,
},
Events = new OpenIdConnectEvents{
OnAuthenticationFailed = OnAuthenticationFailed,
OnAuthorizationCodeReceived = OnAuthorizationCodeReceived,
OnMessageReceived = OnMessageReceived,
OnTicketReceived = OnTicketRecieved,
OnTokenValidated = OnTokenValidated,
OnUserInformationReceived = OnUserInformationReceived,
OnTokenResponseReceived = OnTokenResponseRecieved,
OnRemoteFailure = OnRemoteFailure
}
});
現在App-B
允許委派給App-A
,反之亦然。我也「在試圖訪問一個[Authorize]
的答覆網址爲App-B
,http://localhost:5000/signin-oidc
App-A
」 d控制器,他們必須登錄並從微軟路由到登錄頁面添加App-A
。現在,在此登錄頁面上,我如何立即允許從第一次登錄時訪問App-B
web-api?我可以從最初的握手/登錄中以某種方式發送訪問令牌,或者是進行另一次往返收集類似於下面的令牌的正確方法嗎?
如果我需要再次往返,是否有一個示例代碼塊是最新的?我似乎無法讓代碼塊工作。
所有的例子我發現是那些諸如
private Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext 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);
但我甚至不看code
是上下文的屬性? 我在這個時候使用最新版本的netcore。
我的主要目標是使一個HttpClient
調用App-B
端點從最初的簽署某種方式獲取令牌
當你說的碼流會自動存儲在緩存中的訪問令牌,你的意思是通過調用線以上塊手工檢索:authContext.AcquireTokenByAuthorizationCode?因爲當我試圖排除這一點,並只使用authContext.AcquireTokenSilentAsync,我得到一個錯誤,沒有令牌在緩存中。 – cjsmith
如果您沒有將tokencache實現傳遞給驗證上下文來兌換代碼,那麼您不能在此實例的後續請求中使用AcquireTokenSilentAsync。 – davidcarr
當我的意思是自動,我的意思是來自緩存。 – davidcarr