[英]Why is AllowAnonymous not working while deployed to Azure Websites?
[英]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的博客
在我看來, 除非應用以下四種設置之一, 否則允許在任何頁面上使用匿名:
在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.