AllowAnonymous 不适用于 Azure 广告身份验证

AllowAnonymous is not working with azure ad authentication

我有一个 Asp.net MVC 应用程序,我在其中使用 Azure AD 身份验证对用户进行身份验证。我想让用户无需登录即可访问某些 api 控制器。我尝试将 [AllowAnonymous] 属性放在控制器顶部以跳过这些控制器的身份验证,但它总是重定向到 Microsoft 登录页面以获取凭据。来自 Startup.cs 的代码片段:

public void ConfigureAuth(IAppBuilder app)
    {
        string clientId = GetConfigValue("ida_ClientId");
        string aadInstance = GetConfigValue("ida_AADInstance");
        string tenant = GetConfigValue("ida_Tenant");
        string domain = GetConfigValue("ida_Domain");
        string authority = GetConfigValue("ida_Authority");
        string postLogoutRedirectUri = GetConfigValue("ida_RedirectUri");

        bool devEnvironment = Convert.ToBoolean(GetConfigValue("DevEnvironment"));

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {
            CookieHttpOnly = true,
            CookieSecure = devEnvironment ? CookieSecureOption.SameAsRequest : CookieSecureOption.Always,
        });

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            ClientId = clientId,
            Authority = authority,
            PostLogoutRedirectUri = postLogoutRedirectUri,
            RedirectUri = postLogoutRedirectUri,
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthenticationFailed = context =>
                {
                    context.HandleResponse();
                    context.Response.Redirect("/Error?message=" + context.Exception.Message);
                    return Task.FromResult(0);
                }
            }
        });
    }

    private string GetConfigValue(string key)
    {
        if (RoleEnvironment.IsAvailable)
        {
            return RoleEnvironment.GetConfigurationSettingValue(key);
        }
        else
        {
            return ConfigurationManager.AppSettings[key];
        }
    }
}

如果我遗漏了什么,请告诉我。提前致谢

这是预期的行为。 Easy Auth 是作为本机 IIS 模块实现的,它与您的应用程序在同一沙箱中运行。启用后,发送到 IIS 工作进程的每个 HTTP 请求都必须先通过此模块,然后您的应用程序代码才有机会做出反应。

除非经过身份验证,否则请求将被分派到 Web 应用程序,并且 AllowAnonymous 在这种情况下将不起作用。如果你想允许匿名请求,你可以使用 OWIN 组件来实现身份验证,而不是使用 Easy Auth。

下面是使用 OpenId 组件保护 MVC 的示例:

active-directory-dotnet-webapp-openidconnect

关于Easy Auth的更多细节,可以参考CGillum的博客

Architecture of Azure App Service Authentication / Authorization

在我看来,任何页面都允许匿名除非应用了以下四种设置之一:

在Startup.Authclass的ConfigureAuth方法的底部:

    // This makes any middle-ware defined above this line run before the Authorization rule is applied in web.config
    app.UseStageMarker(PipelineStage.Authenticate);

控制器上的属性 class/method:

    [Authorize]
    public class HomeController : Controller

App_Start 全局过滤器:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new AuthorizeAttribute());
    }
}

在 Web.config

的 system.web 部分
<system.web>
  <authorization>
    <deny users="?" />
  </authorization>
</system.web>

所以您基本上必须通过删除任何全局设置然后要求对视图进行身份验证来限制。