IdentityServer4外部身份验证(Azure Active Directory)失败

时间:2020-03-30 08:57:12

标签: identityserver4 asp.net-core-3.1

我从模板“ is4aspid ”创建了项目,并添加了Azure Active Directory身份验证。 在Startup.cs中添加以下代码:

  // preserve OIDC state in cache (solves problems with AAD and URL lenghts)
  services.AddOidcStateDataFormatterCache();
  // cookie policy to deal with temporary browser incompatibilities
  services.AddSameSiteCookiePolicy();

  services.AddAuthentication()
          .AddOpenIdConnect("aad", "Sign-in with Azure AD", options =>
          {
                    options.Authority = "https://login.microsoftonline.com/common";
                    options.ClientId = "myClientId";
                    options.ClientSecret = "myClientSecret";

                    options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                    options.SignOutScheme = IdentityServerConstants.SignoutScheme;

                    options.ResponseType = "id_token";
                    options.CallbackPath = "/signin-aad";
                    options.SignedOutCallbackPath = "/signout-callback-aad";
                    options.RemoteSignOutPath = "/signout-aad";

                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = false,
                        ValidateAudience = false,
                        NameClaimType = "name",
                        RoleClaimType = "role"
                    };
          });

我在Azure上添加了重定向uri:http://localhost:5000/signin-aad

在调用Callback方法时发生错误。 HttpContext.AuthenticateAsync(IdentityConstants.ExternalScheme)成功始终为 false

[HttpGet]
public async Task<IActionResult> Callback()
{
    var result = await HttpContext.AuthenticateAsync(IdentityConstants.ExternalScheme);
    if (result?.Succeeded != true)
    {
       throw new Exception("External authentication error");
    }
    // some code
}

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

Solution

必须在启动和回调中定义IdentityConstants.ExternalScheme