AllowAnonymous不支持Azure广告身份验证 [英] AllowAnonymous is not working with azure ad authentication

查看:28
本文介绍了AllowAnonymous不支持Azure广告身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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应用程序,除非经过身份验证,AllowAnous在此方案中将不起作用。如果您希望允许匿名请求,则可以使用OWIN组件而不是使用Easy Auth来实现身份验证。

以下是使用OpenID组件保护MVC的示例:

active-directory-dotnet-webapp-openidconnect

有关Easy Auth的更多详细信息,请参阅CGillum的博客

Architecture of Azure App Service Authentication / Authorization

这篇关于AllowAnonymous不支持Azure广告身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆