我有一个 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 应用程序,除非它已通过身份验证并且允许匿名在这种情况下不起作用。如果您想允许匿名请求,您可以使用OWIN组件来实现身份验证,而不是使用Easy Auth。
以下是使用 OpenId 组件保护 MVC 的示例:
活动目录-dotnet-webapp-openidconnect https://github.com/Azure-Samples/active-directory-dotnet-webapp-openidconnect
关于 Easy Auth 的更多细节,可以参考 CGillum 的博客
Azure 应用服务身份验证/授权的体系结构 http://cgillum.tech/2016/02/01/architecture-of-azure-app-service-authentication-authorization/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)