1

我有兩個Azure活動目錄應用程序設置。Azure活動目錄單租戶單獨的webapi應用程序訪問

爲簡潔起見,App-AApp-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-Bhttp://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端點從最初的簽署某種方式獲取令牌

回答

0

上下文對象發生了變化:

context.ProtocolMessage.Code 

碼流自動將將訪問令牌存儲在緩存中,隨後可以使用該緩存:

authContext.AcquireTokenSilentAsync 

更新示例usi NG最新.netcore:

https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore/tree/master/WebApp-WebAPI-OpenIdConnect-DotNet

+0

當你說的碼流會自動存儲在緩存中的訪問令牌,你的意思是通過調用線以上塊手工檢索:authContext.AcquireTokenByAuthorizationCode?因爲當我試圖排除這一點,並只使用authContext.AcquireTokenSilentAsync,我得到一個錯誤,沒有令牌在緩存中。 – cjsmith

+0

如果您沒有將tokencache實現傳遞給驗證上下文來兌換代碼,那麼您不能在此實例的後續請求中使用AcquireTokenSilentAsync。 – davidcarr

+0

當我的意思是自動,我的意思是來自緩存。 – davidcarr

相關問題