作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Asp.net MVC 应用程序,我在其中使用 Azure AD 身份验证对用户进行身份验证。我想允许用户在不登录的情况下访问一些 api Controller 。我尝试将 [AllowAnonymous] 属性放在 Controller 顶部以跳过这些 Controller 的身份验证,但是它总是重定向到 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];
}
}
}
最佳答案
在我看来,任何页面上都允许匿名 除非应用以下四种设置之一:
在 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
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new AuthorizeAttribute());
}
}
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
关于asp.net-mvc - AllowAnonymous 不适用于 azure 广告身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47033151/
我是一名优秀的程序员,十分优秀!