当前方案: Web App和Web API使用AAD B2C进行了身份验证,并且可以正常工作。每个客户都有一个唯一的租户。OpenIdConnectAuthenticationOptions
(web app)和OAuthBearerAuthenticationOptions
(api)在应用程序中设置Startup
。
如此处所述:使用Azure AD B2C对Web App和Web API进行基于令牌的身份验证
不幸的是,必须为每个客户都部署Web应用程序和API,以使它们分开。
要求:为多个客户使用相同的Web应用程序和API,而不是为每个客户部署它们。每个客户将为Web应用程序使用不同的URL。
问题1(Web应用程序):如何根据请求URL将用户重定向(用于身份验证)到正确的租户?
即OpenIdConnectAuthenticationOptions
基于Request.Url
而不是application即时从数据库(或内存)中设置(租户,applicationId,signInPolicy等)Startup
。
问题2(API):如何使用正确的租户来验证收到的令牌?
即OAuthBearerAuthenticationOptions
基于接收的令牌clientId而不是应用程序即时设置配置Startup
根据@ManishJoisar的请求,这就是我很久以前解决问题的方式。这是一个旧的.net Framework 4.8代码示例。
请注意,最近B2C v2策略通过自定义策略提供了更好的联合实施。
public void ConfigureAuth(IAppBuilder app)
{
int index = 2000;
foreach (var record in SystemConfig.ApiToWebUrl)
{
app.MapWhen(
context => System.Web.HttpContext.Current.Request.Url.BaseURL() == record.Key,
config =>
{
var customer = SystemConfig.GetWebSettings(true)[record.Value];
config.UseOAuthBearerAuthentication(CreateBearerOptionsFromPolicy(index, customer.B2CSignInPolicyId,
customer.B2CAadInstance, customer.B2CTenant, customer.B2CApplicationId));
}
);
index++;
}
}
public OAuthBearerAuthenticationOptions CreateBearerOptionsFromPolicy(int index, string b2cPlicyName,
string b2cInstance, string b2cTenant, string b2cClientId)
{
TokenValidationParameters tvps = new TokenValidationParameters
{
// This is where you specify that your API only accepts tokens from its own clients
ValidAudience = b2cClientId,
AuthenticationType = string.Format("{0}_{1}", b2cPlicyName, index)
};
var aadInstance = string.Format("https://{0}{1}", b2cTenant.Split('.')[0], ".b2clogin.com/{0}/{1}/v2.0/.well-known/openid-configuration");
var config = String.Format(aadInstance, b2cTenant, b2cPlicyName);
return new OAuthBearerAuthenticationOptions
{
// This SecurityTokenProvider fetches the Azure AD B2C metadata & signing keys from the OpenIDConnect metadata endpoint
AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider(config)),
Provider = new CustomOAuthBearerProvider()
};
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句