Azure AD B2C调用signin-oidc回调时出现500错误

时间:2017-12-12 16:38:10

标签: azure authentication asp.net-core azure-ad-b2c

我正在编写一个ASP.Net Core 2.0 Web应用程序,我正在尝试使用Azure AD B2C进行身份验证。

public void ConfigureServices(IServiceCollection services)
{
  services.AddDbContext<SchoolContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

  JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

  services.AddAuthentication(options =>
    {
      options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
      options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
      options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
      options.Authority = "https://login.microsoftonline.com";
      options.Audience = "aud";
      options.Events = new JwtBearerEvents
      {
        OnAuthenticationFailed = t =>
        {
          return Task.FromResult(0);
        }
      };
    })
    .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
    {
      options.MetadataAddress = $"https://login.microsoftonline.com/{Configuration["AzureAdB2C:Tenant"]}/v2.0/.well-known/openid-configuration?p={Configuration["AzureAdB2C:Policy"]}";
      options.ClientId = Configuration["AzureAdB2C:ClientId"];
      options.Events = new OpenIdConnectEvents { OnAuthenticationFailed = AuthenticationFailed, OnTokenValidated = Validated, OnRemoteFailure = Failed };
      options.SaveTokens = true;
    });

  services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
  {
    builder.AllowAnyOrigin()
      .AllowAnyMethod()
      .AllowAnyHeader()
      .AllowCredentials();
  }));

  services.AddMvc();

  services.Configure<MvcOptions>(options =>
  {
    options.Filters.Add(new CorsAuthorizationFilterFactory("MyPolicy"));
  });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  app.UseAuthentication();

  if (env.IsDevelopment())
  {
    var builder = new ConfigurationBuilder();
    builder.AddUserSecrets<Startup>();

    app.UseDeveloperExceptionPage();
    app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
    {
      HotModuleReplacement = true,
        ReactHotModuleReplacement = true
    });
  }
  else
  {
    app.UseExceptionHandler("/Home/Error");
  }

  app.UseStaticFiles();
  app.UseCors("MyPolicy");
  app.UseMvc();
}

我在控制器操作上有[Authorize]属性,当我按照链接时,我会看到Microsoft登录页面。我成功登录并将其重定向回https://localhost:[PORT]/signin-oidc,返回500服务器错误。

我的问题是有谁知道为什么会这样?我认为它可能与CORS有关,但看起来并不像它。微软的帖子似乎包含一个有效的令牌。

1 个答案:

答案 0 :(得分:0)

Visual Studio调试窗口通常包含一些有用的信息,可帮助您进行故障排除。

我会更改您的启动代码以匹配this sample(删除对 private static int notification_id=1001; NotificationCompat.Builder mBuilder; mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher3) .setContentTitle(title) .setContentText(DeviceName) .setSubText(timestamp) .setVisibility(visibility) .setPriority(NotificationCompat.PRIORITY_HIGH) .setVibrate(vibrate) .setOngoing(true) .setFullScreenIntent(null, true); Intent resultIntent = new Intent(this, NotificationActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManagerCompat mNotificationManager = (NotificationManagerCompat) NotificationManagerCompat.from(this); Notification notification = mBuilder.build(); notification.defaults |= Notification.DEFAULT_VIBRATE; notification.flags |= Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(notification_id, notification); startForeground (notification_id, notification) notification_id = notification_id + 1; 的调用):

AddOpenIdConnect()

我还会在services.AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(jwtOptions => { jwtOptions.Authority = $"https://login.microsoftonline.com/tfp/{Configuration["AzureAdB2C:Tenant"]}/{Configuration["AzureAdB2C:Policy"]}/v2.0/"; jwtOptions.Audience = Configuration["AzureAdB2C:ClientId"]; jwtOptions.Events = new JwtBearerEvents { OnAuthenticationFailed = AuthenticationFailed }; }); 中为OnMessageReceived添加处理程序。在该处理程序上放置一个断点,看看你是否已经走得那么远。