GetExternalLoginInfoAsync 始终返回 null Azure AD

时间:2021-07-31 20:16:20

标签: c# asp.net-core authentication openid-connect azure-ad-b2b

GetExternalLoginInfoAsync 始终返回 null。 我尝试将 Azure AD 集成到 Identity。

例如,我可以通过硬编码添加 loginProvider 吗?

我的启动:

services.AddIdentity<IdentityUser, IdentityRole>()
         .AddEntityFrameworkStores<IdentityContext>()
         .AddDefaultTokenProviders();
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
         .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
services.AddControllersWithViews();
services.AddRazorPages()
         .AddMicrosoftIdentityUI();

这是一个简短的列表,我也使用自定义中间件。

  app.UseHttpsRedirection();
  app.UseStaticFiles();
  app.UseRouting();
  app.UseAuthentication();
  app.UseAuthorization();
  app.UseEndpoints(endpoints =>
  {
    endpoints.MapControllerRoute(
      name: "default",
      pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
  });

我通过点击按钮调用挑战

[HttpGet]
public ChallengeResult ExternalSignIn()
{
  var redirectUrl = Url.Action(nameof(ExternalLoginCallback));
  var properties = _signInManager.ConfigureExternalAuthenticationProperties
                   (OpenIdConnectDefaults.AuthenticationScheme, redirectUrl);
  return new ChallengeResult(OpenIdConnectDefaults.AuthenticationScheme, properties);
}

挑战调用:

[HttpGet]
public async Task<IActionResult> ExternalLoginCallback()
{
  //info is null
  var info = await _signInManager.GetExternalLoginInfoAsync().ConfigureAwait(false);
  var result = await _signInManager.ExternalLoginSignInAsync(
                     info.LoginProvider, 
                     info.ProviderKey, 
                     isPersistent: false, 
                     bypassTwoFactor: false)
                     .ConfigureAwait(false);

  //other code
}      

1 个答案:

答案 0 :(得分:2)

services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme,
                options => options.SignInScheme = IdentityConstants.ExternalScheme);
相关问题