Wednesday, 23 May 2012

windows authentication in iis 7 ASP.NET 4

Step 1)  Create new Website choose .NET 4.0  and empty website. It must be created in IIS 7/7.5
Step 2) Create another (virtual) directory  called UPLOADS and add aspx file + web.config as shown in the picture.

Step 3)  Edit Web.config file

      <system.web>
      <authentication mode="
Windows"></authentication>
      <identity impersonate="
true" />
    </system.web>


Step 4) User should disable Anonymous Authentication (User can change using inetmgr.exe)
          or add configuration entries in web.config file as shown below.
<system.webServer>

 <security>
<authentication>
        <anonymousAuthentication enabled="false" />
        <windowsAuthentication enabled="true" />
</authentication>
</security>


<validation validateIntegratedModeConfiguration="false" />
</system.webServer>

Step 5)  Add   default.aspx in uploads directory
//change Page_load event ...



    protected void Page_Load(object sender, EventArgs e)
    {
Response.Write("Authentication Type="+HttpContext.Current.User.Identity.AuthenticationType+"<br/>");
Response.Write("ISAuthenticated="+HttpContext.Current.User.Identity.IsAuthenticated+"<br/>");
Response.Write("Logged in username="+HttpContext.Current.User.Identity.Name + "<br/>");
    }



OUTPUT