[英]AllowAnonymous only works intermittently in ASP.NET MVC 5
我的一個控制器操作拒絕進行身份驗證,我無法理解為什么。 它只是Visual Studio中標准MVC 5項目模板的標准VerifyCode操作,它看起來像這樣:
[AllowAnonymous]
public async Task<ActionResult> VerifyCode(string provider, string returnUrl, bool rememberMe)
{
if (!await SignInManager.HasBeenVerifiedAsync())
{
return View("Error");
}
return View(new VerifyCodeViewModel { Provider = provider, ReturnUrl = returnUrl, RememberMe = rememberMe });
}
每當我的應用程序在沒有用戶登錄的情況下命中該控制器時,它就會將用戶發送回登錄屏幕,即使它使用AllowAnonymous進行修飾。 相比之下,這個標准的控制器動作:
[AllowAnonymous]
public ActionResult ForgotPassword()
{
return View();
}
如果沒有登錄用戶直接點擊,則工作正常。
因此,為了弄清楚發生了什么,我向控制器添加了以下測試操作:
// to see if it's down to the parameters
[AllowAnonymous]
public ActionResult VerifyCode()
{
return View(new VerifyCodeViewModel { Provider = "", ReturnUrl = "", RememberMe = false });
}
// to see if it's down to the action name
[AllowAnonymous]
public ActionResult VerifyCod()
{
return View(new VerifyCodeViewModel { Provider = "", ReturnUrl = "", RememberMe = false });
}
// to see if it's down to the viewmodel
[AllowAnonymous]
public ActionResult ForgotPassword()
{
return View(new VerifyCodeViewModel { Provider = "", ReturnUrl = "", RememberMe = false });
}
點擊前兩個導致我的應用程序將我發送回登錄頁面。 點擊第三個呈現ForgotPassword視圖沒有問題。
我沒有任何自定義授權過濾器。 到底是怎么回事?
檢查.vs / config / applicationhost.config文件並驗證以下部分設置為Deny和true。
<section name="anonymousAuthentication" overrideModeDefault="Deny" />
<add name="AnonymousAuthenticationModule" lockItem="true" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.