我阅读了这篇文章:Secure ASP.NET Web API 2 using Azure AD B2C – Part 2
在第3步,app.UseOAuthBearerAuthentication
被调用3次,每个策略一次。
我仅使用两种策略进行测试:SignInPolicyId
和SignUpPolicyId
。
但是我什么时候做:
app.UseOAuthBearerAuthentication(CreateBearerOptionsForPolicy(SignInPolicyId))
app.UseOAuthBearerAuthentication(CreateBearerOptionsForPolicy(SignUpPolicyId));
并将其Provider
传递给OnValidateIdentity
来处理验证声明等。然后,我尝试按以下策略进行过滤:
private OAuthBearerAuthenticationOptions CreateBearerOptionsForPolicy(string policy)
{
var metadataEndpoint = string.Format(AadInstance, TenantId, policy);
TokenValidationParameters tvps = new TokenValidationParameters
{
// This is where you specify that your API only accepts tokens from its own clients
ValidAudience = ClientId,
AuthenticationType = policy,
NameClaimType = "http://schemas.microsoft.com/identity/claims/objectidentifier",
ValidateIssuer = true
};
return new OAuthBearerAuthenticationOptions
{
AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider(metadataEndpoint)),
Provider = new OAuthBearerAuthenticationProvider
{
OnValidateIdentity = async context =>
{
try
{
var policyName = identity.FindFirst("http://schemas.microsoft.com/claims/authnclassreference")?.Value;
if (policyName == B2CSignInPolicyId.ToLower()) // Sign In Only policy...
{
// Run specific code here for the policy that just sent a token back to the application...
}
问题:正在@ Azure B2C上执行的策略,即我正在调用的策略是Sign In
策略,但是提供程序代码被调用了两次。我为每项政策(包括Sign Up
)注册了一次。
问题:还有什么更好的方法可以使我们正确地执行Provider吗?如果正在调用Sign In
策略,则仅执行Sign In
提供程序,反之亦然。
编辑:
我遇到了此处描述的相同问题(顺序包含多个元素):http://bitoftech.net/2016/08/24/secure-aspnet-web-api-2-azure-ad-b2c/#comment-96913
这是例外:
在System.Linq.Enumerable.SingleOrDefault [TSource](IEnumerable`1 来源)\ r \ n位于 Microsoft.Owin.Security.AuthenticationManager.d__20.MoveNext()\ r \ n --- 从先前引发异常的位置开始的堆栈跟踪结束 --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n System.Web.Http.HostAuthenticationFilter.d__4.MoveNext()\ r \ n --- 从先前引发异常的位置开始的堆栈跟踪结束 --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n System.Web.Http.Controllers.AuthenticationFilterResult.d__5.MoveNext()\ r \ n --- 从先前引发异常的位置开始的堆栈跟踪结束 --- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\ r \ n System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\ r \ n System.Web.Http.Controllers.ExceptionFilterResult.d__6.MoveNext()
答案 0 :(得分:0)