[英]MVC3 Application (Windows Authentication) enable anonymous access
我使用VS2012創建了一個MVC3內聯網應用程序。 它使用Windows身份驗證。 它在我的本地IIS上運行。 IIS已啟用“匿名和Windows身份驗證”。
一旦運行該應用程序,就會出現一個登錄彈出窗口。
HomeController的:
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
的TestController:
[Authorize]
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
我想知道如何僅在指定[Authorize]屬性時對用戶進行身份驗證...換句話說,當用戶進入站點(主頁)時,不需要登錄。 當他們導航到TestController時,他們必須登錄。
您可以像這樣明確地指定它:
[AllowAnonymous]
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
可以將authorize屬性放在類級別或操作級別上。 如果將其放置在類級別,則每個操作都將要求您登錄。如果將其放置在操作級別上,則僅該特定操作將需要用戶認證。
[Authorize]
public class CustomerServiceController
{
....
}
public class AccountController
{
[AllowAnonymous]
public ActionResult Index()
{
....
}
[Authorize]
public ActionResult Accounts()
{
....
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.