简体   繁体   中英

Bypass windows authentication on a controller MVC

I've enabled Windows authentication on my mvc web application but I need it to be bypassed on the first page(so anyone can access HomeController\\Index).How can this be achieved?

Here's my authentication logic:

<location path="~/DashboardController" />
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Windows" />
    <authorization>
      <allow users="Domain\MyUserName"/>
      <deny users="*" />
    </authorization>
  </system.web>

I've tried decorating the default dashboard controller Action with [Authorize] and [Authorize(Users="*")] but when I try access the home page the browser displays a prompt to enter credentials

Add the [AllowAnonymous] attribute to your Index method

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

    return View();
}

Or if you don't want to use attributes for what ever reason you can allow this via the web.config. (I'd avoid the web.config where possible and use attributes. See this blog from Jon Galloway for more info )

<location path="HomeController/Index">
    <system.web>
        <authorization>
            <allow users="?" />
        </authorization>
    </system.web>
</location>

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