Mediator
Mediator

Reputation: 15378

How work with Active Directory in ASP.NET MVC

I apologize that I was creating the question that already have the answers, but I do not understand.

I need a simple application that will use the AD and use user roles.

First with what I have a problem to get the connection string. I was given a user, and said that it connects so:

LDAP://CN=User_1,OU=Test,OU=ADTestOU,DC=tv-tel,DC=local

What to put into web.config ?

  <connectionStrings>
    <add name="ADConnectionString" connectionString="LDAP://CN=User_1,OU=Test,OU=ADTestOU,DC=tv-tel,DC=local" />
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <!--Make sure you have fomrs authentication enabled.-->
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <authorization>
      <deny users="?"/>
      <allow users="*"/>
    </authorization>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="MyADMembershipProvider">
      <providers>
        <add
        name="MyADMembershipProvider"
        type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        connectionStringName="ADConnectionString"
        connectionProtection="Secure"
        connectionUsername="user"
        connectionPassword="pwd"
        attributeMapUsername="sAMAccountName"
        enableSearchMethods="true"
        attributeMapEmail="mail"
/>
      </providers>
    </membership>

    <roleManager defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </roleManager>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>
  </system.web>

Upvotes: 0

Views: 989

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

If you want to work with Windows Authentication in AD you need to use <authentication mode="windows" /> in your web.config instead of forms.

Try creating a new ASP.NET MVC 3 application using the Intranet Application template and it will set it up for you.

Also make sure that you have properly configured IIS and enabled Windows Authentication. When you create a new project using the intranet template it will generate a text file for you that explains how to set it up step by step.

Upvotes: 1

Related Questions