Azure 活动目录单租户单独的 webapi 应用程序访问
Azure active directory single tenant separate webapi application access
我有两个 Azure 活动目录应用程序设置。
为简洁起见,App-A
和 App-B
。
A
是一个网络应用程序,在本地主机上运行,而 B
发布到 somedomain.azurewebsites.net
并公开一个网络 api.
两者都设置为:
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
,反之亦然。我也把App-A
的回复URL加到App-B
,http://localhost:5000/signin-oidc
App-A
在尝试访问 [Authorize]
的控制器时,他们必须登录并路由到 Microsoft 的登录页面。现在在这个登录页面上,我如何才能在第一次登录时立即允许访问 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。
我的主要目标是使用从初始登录以某种方式检索到的令牌对 App-B
端点进行 HttpClient
调用。
上下文对象已更改:
context.ProtocolMessage.Code
代码流会自动将访问令牌存储在缓存中,您随后可以使用以下方法检索该令牌:
authContext.AcquireTokenSilentAsync
使用最新的 .netcore 更新示例:
我有两个 Azure 活动目录应用程序设置。
为简洁起见,App-A
和 App-B
。
A
是一个网络应用程序,在本地主机上运行,而 B
发布到 somedomain.azurewebsites.net
并公开一个网络 api.
两者都设置为:
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
,反之亦然。我也把App-A
的回复URL加到App-B
,http://localhost:5000/signin-oidc
App-A
在尝试访问 [Authorize]
的控制器时,他们必须登录并路由到 Microsoft 的登录页面。现在在这个登录页面上,我如何才能在第一次登录时立即允许访问 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。
我的主要目标是使用从初始登录以某种方式检索到的令牌对 App-B
端点进行 HttpClient
调用。
上下文对象已更改:
context.ProtocolMessage.Code
代码流会自动将访问令牌存储在缓存中,您随后可以使用以下方法检索该令牌:
authContext.AcquireTokenSilentAsync
使用最新的 .netcore 更新示例: