【问题标题】:OpenIdConnectAuthenticationHandler: message.State is null or empty. when connecting to Azure Active DirectoryOpenIdConnectAuthenticationHandler:message.State 为 null 或为空。连接到 Azure Active Directory 时
【发布时间】:2016-12-14 21:11:53
【问题描述】:

我正在尝试使用我的应用程序连接到 Azure Active Directory,但出现以下错误: 处理请求时发生未处理的异常。

异常:OpenIdConnectAuthenticationHandler:message.State 为空 或为空。地点不明

AggregateException:未处理的远程故障。 Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.d__5.MoveNext()

我的 appsettings.json 文件如下所示:

"AzureAd": {
    "ClientId": "<Application ID value from Azure Portal>",
    "AadInstance": "https://login.microsoftonline.com/{0}",
    "TenantId": "<tenant>.onmicrosoft.com",
    "AuthCallback": "/"
  }

在 Startup.cs 中

  app.UseCookieAuthentication();

  app.UseIdentity();
  app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
  {
       ClientId = Configuration["AzureAd:ClientId"],
       Authority = string.Format(Configuration["AzureAd:AadInstance"], Configuration["AzureAd:TenantId"]),
       CallbackPath = Configuration["AzureAd:AuthCallback"]
  });

我在我的项目中启用了 ssl,并添加了 Visual Studio 给我的 url 作为回复 URL。

之前,当我配置了错误的 URL 时,它会引导我进入 Microsoft 登录站点,该站点说该 URL 与任何已注册的 URL 都不匹配,所以我知道它至少有那么远。

我也应该说我在关注this post,但使用的是新门户。我无权访问经典门户。

我该如何解决这个错误?

感谢您的帮助。

【问题讨论】:

  • 你解决了这个问题吗?

标签: c# azure asp.net-core asp.net-core-mvc azure-active-directory


【解决方案1】:

我不确定它是否相关,但我遇到了类似的问题,并带有类似的错误消息。 @rfcdejong 通过建议调试 OnMessageReceived 为我指明了正确的方向。就我而言,我试图在 Azure AD B2C 中实现重置密码自定义流程。它只用一个 id_token 调用它。这让我意识到此时我应该将用户重定向到主页 - 否则我还会收到 message.State is null or empty 错误。

我的代码如下所示: 启动.cs

services.Configure<OpenIdConnectOptions>(AzureADB2CDefaults.OpenIdScheme, options =>
{
    options.EventsType = typeof(CustomOpenIdConnectEvents);
});

然后在 CustomOpenIdConnectEvents 我有:

    public class CustomOpenIdConnectEvents : OpenIdConnectEvents
    {
        ...

        public override async Task MessageReceived(MessageReceivedContext context)
        {
            // Set breakpoint in here to see what is happening

            if (!string.IsNullOrEmpty(context.ProtocolMessage.Error) && !string.IsNullOrEmpty(context.ProtocolMessage.ErrorDescription))
            {
                if (context.ProtocolMessage.ErrorDescription.StartsWith("AADB2C90118")) // forgot password
                {
                    context.HandleResponse();
                    context.Response.Redirect("/Account/ResetPassword");
                }
                else
                {
                    context.HandleResponse();
                    context.Response.Redirect("/");
                }
            }
            else
            {
                // On successful password reset we need to do this or we get a message.State is null or empty
                context.HandleResponse();
                context.Response.Redirect("/");
            }

            return;// Task.FromResult(0);

        }

值得注意的是,我的做法与大多数人略有不同。通常,您不会使用继承自 OpenIdConnectEvents 的自定义对象覆盖整个 EventsType。相反,您可以这样做,全部在 Startup.cs 文件中:

services.Configure<OpenIdConnectOptions>(options => 
{

    // ...

    options.Events = new OpenIdConnectEvents
    {
        OnMessageReceived = context =>
        {
            if (context.HttpContext.Request.Query.ContainsKey("error"))
            {
                context.HandleResponse(); // <-- Fires
                context.Response.Redirect("/AccessDenied"); // <-- Redirect fires but user is not redirected
            }

            return Task.FromResult(0);
        }
    }
}

而且,如果调试 OnMessageReceived 没有帮助,您也可以试试 OnRemoteFailureOnAuthenticationFailed

【讨论】:

    【解决方案2】:

    当您提到新门户时,请确保它指的是new Azure portal 而不是new register apps portal 用于Azure AD v2.0 endpoint

    之前,当我配置了错误的 URL 时,它会引导我进入 Microsoft 登录站点,该站点说该 URL 与任何已注册的 URL 都不匹配,所以我知道它至少有那么远。

    您在代码中使用的 redirect_url 应针对您在 Azure 上注册的应用进行配置。

    我也关注了这个链接,这个项目对我来说效果很好。您可以从here 下载该项目。请先在Azure上为应用配置redirect_url(https://localhost:44397/signin-aad)并根据你的应用设置appsetting,然后你就可以使用Url访问应用了https://localhost:44397

    【讨论】:

      【解决方案3】:

      如果有人遇到这种情况,请将事件处理程序添加到 OnMessageReceived 以调试请求。不知何故,我偶然发现将 redirect_url 设置为 /signin-oidc 并且该路径上的 GET 将有一个空的上下文。Properties 这当然会导致这个特定的错误

      【讨论】:

      • 你能举个例子吗?
      猜你喜欢
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2014-01-06
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多