【问题标题】:AllowAnonymous is not working with azure ad authenticationAllowAnonymous 不适用于 azure 广告身份验证
【发布时间】:2018-04-12 11:44:38
【问题描述】:

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

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];
        }
    }
}

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

【问题讨论】:

  • 您是否将其部署到 Azure 并启用了easy authentication
  • 嗨@FeiXue,感谢您的帮助,在azure中部署代码后也无法正常工作。您也可以在easy authentication 上分享一些带有示例的链接。提前致谢。

标签: asp.net-mvc owin azure-active-directory openid-connect


【解决方案1】:

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

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

这是一个使用 OpenId 组件保护 MVC 的示例:

active-directory-dotnet-webapp-openidconnect

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

Architecture of Azure App Service Authentication / Authorization

【讨论】:

    【解决方案2】:

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

    在 Startup.Auth 类的 ConfigureAuth 方法的底部:

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

    控制器类/方法的属性:

        [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>
    

    因此,您基本上必须通过删除任何全局设置然后要求对视图进行身份验证来限制进行反向思考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-17
      • 2016-08-04
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多