ASP.NET MVC with Simple Windows Authorization

A lot of enterprises use Active Directory (AD) to manage user accounts and Security Groups to manage access to resources.

So (I think) that there is a common task when you want to create some internal resource that will provide certain functionality for your team, but you do not want to expose your data outside. We can easily enable Windows authentication, however usually we also need to add an authorization(limit access to certain groups)

The task is simple, but I do not know why it is so hard to find manual for this. Steps are as follows:

  • Enable Windows authentication in web.config
  • Add WindowsTokenRoleProvider that transforms all Security Groups to ASP.NET Roles
  • Configure Authorization rules based on roles
  • Disable anonymous authentication for IIS Express.

Changes in Web.config:

<?xml version="1.0" encoding="utf-8"?>
    <authentication mode="Windows" />
      <allow roles="DOMAIN\MyTeam" />
      <deny users="*"/>
    <roleManager cacheRolesInCookie="false" defaultProvider="WindowsProvider" enabled="true">
        <clear />
        <add name="WindowsProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />

Changes in project file:

<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="">
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <IISExpressSSLPort />
    <IISExpressUseClassicPipelineMode />
    <UseGlobalApplicationHostFile />

P.S. You can use security groups to restrict access to Controllers/Views based on the roles (AuthorizeAttribute)