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

相关问题