Asp.net-Mvc
AllowAnonymous 屬性不起作用 MVC 5
在 Azure 門戶中,我為我的 Web 應用程序設置了“打開”應用服務身份驗證,並使用 AAD 作為身份驗證提供程序。
到目前為止,這一直很好,我需要一個允許匿名使用者的端點,但是該屬性
[AllowAnonymous]不起作用,我仍然需要登錄。程式碼:
[Authorize] [RoutePrefix("users")] public class UsersController : Controller { [Route("register/{skypeid}")] public ActionResult Register(string skypeid) { ///stuff... } catch (Exception ex) { return Content(ex + ""); } ViewBag.Name = name; return View(); } [AllowAnonymous] [Route("exists/{skypeid}")] public ActionResult Exists(string skypeid) { return Content("Hello " + skypeid); }我認為程式碼是正確的,那麼它與我為我的 Web 應用程序使用應用服務身份驗證這一事實有關嗎?
編輯:所以,我找到了問題的根源,在 Azure 中,如果將“未經過身份驗證時採取的操作”設置為“使用 Azure Active Directory 登錄”,它永遠不允許匿名。
但是,如果我將其更改為允許匿名,則在嘗試使用
$$ Authorize $$-屬性,它只是告訴我“您無權查看此目錄或頁面。” 這是故意的嗎?看起來真的很奇怪。如果有,我希望使用者被重定向到登錄$$ Authorize $$-屬性。 為清晰起見的螢幕截圖:
我剛剛在我的書 - http://aka.ms/zumobook中寫過這個- 在第 6 章中查看 MVC 部分。
它的基本要點是您需要做更多的事情來啟用身份驗證;最具體地說,您需要設置身份驗證管道(Azure 移動應用伺服器 SDK 將為您完成此操作),並且您需要在 Web.config 中設置表單重定向:
<system.web> <compilation debug="true" targetFramework="4.5.2"/> <httpRuntime targetFramework="4.5.2"/> <authentication mode="Forms"> <forms loginUrl="/.auth/login/aad" timeout="2880"/> </authentication> </system.web>由於將移動應用 SDK 添加到 ASP.NET 應用程序有幾個細節,我將參考參考章節了解這些細節。
如果有,請檢查您的 web.config
<authorization> <deny users="?" /> </authorization>它的覆蓋
$$ AllowAnonymous $$ 添加
<location path="YourController/AnonymousMethod"> <system.web> <authorization> <allow users="*"/> </authorization> </system.web> </location>允許匿名訪問

