简体   繁体   English

AllowAnonymous无法使用azure广告验证

[英]AllowAnonymous is not working with azure ad authentication

I have a Asp.net MVC application in which I am using Azure AD authentication to authenticate the users. 我有一个Asp.net MVC应用程序,我在其中使用Azure AD身份验证来验证用户。 I want to allow users to access some of the api controller without login. 我想允许用户无需登录即可访问某些api控制器。 I tried putting [AllowAnonymous] attribute on top of the controllers to skip these controllers from authentication, however its always redirecting to microsoft login page for credentials. 我尝试在控制器之上放置[AllowAnonymous]属性以从身份验证中跳过这些控制器,但是它总是重定向到Microsoft登录页面以获取凭据。 Code snippet from Startup.cs: 来自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];
        }
    }
}

Please let me know if I am missing anything. 如果我遗失任何东西,请告诉我。 Thanks in advance 提前致谢

This is expected behavior. 这是预期的行为。 Easy Auth is implemented as a native IIS module that runs in the same sandbox as your application. Easy Auth实现为本机IIS模块,与您的应用程序在同一个沙箱中运行。 When enabled, every HTTP request dispatched to the IIS worker process must first pass through this module before your application code has a chance to react. 启用后,调度到IIS工作进程的每个HTTP请求必须首先通过此模块,然后才能对应用程序代码作出反应。

The request will be dispatched to the web app unless it is authenticated and the AllowAnonymous will not work in this scenario. 该请求将被分派到Web应用程序,除非它已通过身份验证,并且AllowAnonymous在此方案中不起作用。 If you want to allow the anonymous request, you can implement the authentication using OWIN component instead of using the Easy Auth. 如果要允许匿名请求,可以使用OWIN组件而不是使用Easy Auth来实现身份验证。

Here is an example protect the MVC with OpenId component: 这是一个使用OpenId组件保护MVC的示例:

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

And more detail about Easy Auth, you can refer the CGillum's blog 有关Easy Auth的更多细节,您可以参考CGillum的博客

Architecture of Azure App Service Authentication / Authorization Azure应用服务身份验证/授权的体系结构

It seems to me like Anonymous is allowed on any page unless one of these four settings are applied: 在我看来, 除非应用以下四种设置之一, 否则允许在任何页面上使用匿名:

At the bottom of the ConfigureAuth method of the Startup.Auth class: 在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);

Attribute on the controller class/method: 控制器类/方法的属性:

    [Authorize]
    public class HomeController : Controller

App_Start global filter: App_Start全局过滤器:

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

In the system.web section of Web.config 在Web.config的system.web部分中

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

So you basically have to think in reverse by removing any global settings and then requiring authentication on the views to restrict. 因此,您基本上必须反过来考虑删除任何全局设置,然后要求对视图进行身份验证以进行限制。

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

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