繁体   English   中英

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的示例:

主动目录的dotnet-web应用,openidconnect

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

Azure应用服务身份验证/授权的体系结构

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM