简体   繁体   中英

MVC3 Application (Windows Authentication) enable anonymous access

I created a MVC3 Intranet Application using VS2012. It uses Windows authentication. It runs on my Local IIS. IIS has Anonymous & Windows Authentication Enabled.

As soon as I run the application, a login popup appears.

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();
}

I would like to know what to do in order to have the user Authenticated only when the [Authorize] attribute is specified... In other words, when the user enters the site (Home), no login is required. When they navigate to the TestController, they have to log on.

You specify it explicitly like this:

[AllowAnonymous]
public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    return View();
}

The authorize attribute can be placed on class level or on action level. If you place it class level every action will require you to be logged in. If you place it on the action level only that specific action will require user authentication.

[Authorize]
public class CustomerServiceController
{
     ....
}

public class AccountController
{
    [AllowAnonymous]
    public ActionResult Index()
    {
        ....
    }

    [Authorize]
    public ActionResult Accounts()
    {
        ....
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM