Click here to Skip to main content
12,453,183 members (62,511 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Basic Authentication with the ASP.NET Membership API

, 22 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
In this post we’ll discuss how you can get IIS to use basic authentication against an ASP.NET Membership store.

If you are an ASP.NET developer you are probably familiar with both membership and http basic authentication. If you are familiar with IIS’s different authentication methods you also know that the only way to use HTTP authentication on IIS to verify the credentials against the windows account store. In this post we’ll discuss how you can get IIS to use basic authentication against an ASP.NET Membership store.

The second question you should ask yourself, why would you want to do this? After all basic authentication has some serious drawbacks, most namely that credentials are send as plain text when you don’t use SSL, and you can’t force users to sign out due to HTTP’s stateless nature (most browsers cache the credentials until you close the browser window).

Next to that there are also some benefits to using basic authentication in combination with IIS. Namely: it provides an generic interface for authentication supported by all clients that implement the HTTP protocol, using membership you can share credentials over several web applications, it is platform independent (it doesn’t matter if you use classic asp, php, or anything else that runs on IIS).

So to be more concrete, how do we implement this feature? We implement an System.Web.IHttpModule and handle the AuthenticateRequest event. For more details on how to implement this download the attachment to this post.

The module can be configured with the following options:

<authenticationSection xmlns="urn:BasicAuthenticationModule" enabled="true" realm="test"
requireAuthentication="true" requireSsl="false">
<cache enableCache="false" cacheDurationMinutes="1"/>
<roles enableRoles="true" applicationRoleName="sample"/>
  • /authenticationSection/enabled defines to use the module or not
  • /authenticationSection/realm the text to be displayed in the login dialog
  • /authenticationSection/requireAuthentication allow both anonymous and authenticated users to login (your application logic can then handle these situations accordingly.
  • /authenticationSection/requireSsl if set to true users can’t authenticated if the request wasn’t made over an https request
  • /authenticationSection/cache/enableCache if enabled the authentication key of the user is cached for the defined period. You might want to enable this because http is stateless every request to the server is authenticated, thus making a round trip to the database. This include css files, images and javascript files so it can be easily 15 database requests per page without caching.
  • /authenticationSection/cache/cacheDuration the duration of the cache in minutes
  • /authenticationSection/roles/enableRoles Enable your application to roles this feature is useful when you share your membership database over several applications, you can then assign every application as a role, allowing you to configure to which applications a user has access.
  • /authenticationSection/roles/applicationRoleName The name of the role/application that will be used to assign rights to users.

To start using the application you have to either copy the .dll in the download to the bin folder or add the dll to the Global Assembly Cache so that all your applications can use it in IIS. Next you have to make sure you have the following sections inclulded and configured in your web.config file:

<?xml version="1.0" encoding="utf-8"?>
      <section name=”authenticationSection” type=”BasicAuthenticationModule.AuthenticationSection, BasicAuthenticationModule”/>
   <authenticationSection xmlns=”urn:BasicAuthenticationModule” enabled=”true” realm=”dex test” requireAuthentication=”true” requireSsl=”false”> 
      <cache enableCache=”false” cacheDurationMinutes=”1″/>
      <roles enableRoles=”true” applicationRoleName=”sample”/>
      <add name=”ApplicationServices” connectionString=”data source=.;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true” providerName=”System.Data.SqlClient” />
         <clear />
         <add name=”AspNetSqlMembershipProvider” type=”System.Web.Security.SqlMembershipProvider” connectionStringName=”ApplicationServices” enablePasswordRetrieval=”false” enablePasswordReset=”true” requiresQuestionAndAnswer=”false” requiresUniqueEmail=”false” maxInvalidPasswordAttempts=”5″ minRequiredPasswordLength=”6″ minRequiredNonalphanumericCharacters=”0″ passwordAttemptWindow=”10″ applicationName=”/” />
   <roleManager enabled=”true”>
      <clear />
      <add name=”AspNetSqlRoleProvider” type=”System.Web.Security.SqlRoleProvider” connectionStringName=”ApplicationServices” applicationName=”/” />
   <validation validateIntegratedModeConfiguration=”false” />
   <modules runAllManagedModulesForAllRequests=”true”>
   <add name=”CustomBasicAuthentication”

       type=”BasicAuthenticationModule.AuthenticationModule, BasicAuthenticationModule” />

I’m assuming you already now how to setup the membership database. If not you can find more information at the msdn page on configuring membership.

Download The Basic Authentication Module for IIS
Use this framework to configure Membership Users or integrate Membership into your ASP.NET MVC Application.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Dexchange Outsourcing
Netherlands Netherlands
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 22 Feb 2012
Article Copyright 2012 by wullinkm
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid