ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

I usually try and spend at least an hour or two each night hanging out on the ASP.NET Forums answering questions.  The last week or so I’ve been spending a lot of time in the Security Forum answering a lot of “how to” questions about some of the new features in ASP.NET 2.0 (in particular the new ASP.NET 2.0 Membership, Role Management and Login Controls) and pointing people at resources and samples around the web about them.  What I thought I’d try and do with this post is consolidate a lot of pointers to different ASP.NET 2.0 security resources in one place.

 

Getting Started with ASP.NET 2.0 Membership, Roles and Forms Authentication Video

 

If you haven’t watched this great online video yet you absolutely should.  It walks through how to add Forms Authentication (using the <asp:login> control) with a secure Membership Credential Store + Role Based Security to a site, then implement pages that enable Registration (using the <asp:createuserwizard> control) + Change Password (using the <asp:changepassword> control) + Reset Password (using the <asp:recoverypassword> control), and then authorize page access and hide menu navigation links using the role groupings of the authenticated user.  The video shows how to-do all of this from scratch in only 17 minutes.  You can watch it here.  You can also find other great ASP.NET “how to” videos here.

 

ASP.NET 2.0 Membership and Role Management Overview Articles

 

Here are a few good tutorial articles that provide a good conceptual overview of how the new membership and role management system works. 

 

Scott Mitchell’s: Examining ASP.NET 2.0’s Membership, Roles and Profile (Part 1)

Scott Mitchell’s: Examining ASP.NET 2.0’s Membership, Roles and Profile (Part 2)

Scott Mitchell's: Examining ASP.NET 2.0's Membership, Roles and Profile (Part 3)

Scott Mitchell's: Examining ASP.NET 2.0's Membership, Roles and Profile (Part 4)

Scott Mitchell's: Examining ASP.NET 2.0's Membership, Roles and Profile (Part 5) 

 

MSDN: Explained: Forms Authentication in ASP.NET 2.0

MSDN: Explained: Windows Authentication in ASP.NET 2.0

 

Scott Allen’s: Membership Providers (Part 1)

Scott Allen’s: Role Providers (Part 2)

 

ASP.NET 2.0 Security, Membership and Role Management Book

 

Stefan Schackow is the ASP.NET Team technical expert and feature-owner for a lot of the core sub-systems in ASP.NET, and he owned the security, membership and role management features for ASP.NET 2.0.  He has recently published an awesome book on ASP.NET Security, Membership and Roles that you can buy for $26 on Amazon here. 

 

 

You can read two big recommendations of it from ASP.NET MVPs here: Dave Sussman and Christoph Wille  I highly recommend getting a copy.

 

Setting up Membership + Roles on a SQL 2000 or SQL 2005 Server

 

By default ASP.NET 2.0 auto-creates and uses a SQL Express database to store Membership, Roles and Profile data.  If you want to instead use a SQL 2000 or SQL 2005 database, you can easily learn how to configure it using this blog post of mine.

 

Don't forget to always set the "applicationName" attribute when configuring ASP.NET Membership, Roles, Profile and other providers.

 

One common issue people forget to-do when registering membership and other providers is to configure the "applicationName" attribute on the provider declaration.  This can prevent logins from seeming to work when you copy an application to another machine.  This blog post covers this scenario more and how to fix it.

 

Custom Membership and Roles Providers

 

ASP.NET 2.0 ships with built-in SQL Server, SQL Express and Active Directory Membership and Role Providers.  The source code for these built-in providers can now be downloaded from here.

 

The nice thing about the system is that it is entirely extensible, which means you can create and configure your own custom credential/role stores into the system as well (either using the source code from the built-in providers, or just by extending the provider contract). 

 

The ASP.NET Provider Toolkit Site provides tons of content on how to create and build your own providers (including Membership and Role Providers).  It also has a link to a fully functional Membership and Role Provider that works with Access databases.  This article also discusses how to build your own Membership Provider, and can be a useful guide to integrating the membership APIs with your own existing database.

 

Here is a list of other free custom Membership and Roles providers (with complete source code) that I know of on the web:

You can download and configure your application to use any of the above providers.  The beauty of the system is that the Membership, Roles APIs + Login Controls don't change at all. 

 

Storing Custom Properties about a User during Registration

 

One very common question I see asked a lot is how to store custom properties about a new user as they register on the system (example: zip code, gender, etc).  The good news is that it is easy to-do this with the new ASP.NET Profile System and the built-in <asp:createuserwizard> control. 

 

I have a sample here that shows how to build a registration system for a site with Membership, Login, Registration, Password Recovery, Change Password, Custom Properties and Roles support – all in 24 lines of code.  If you want, you can combine this with the new SQLTableProvider for the Profile system for greater control over your profile database schema.  You can learn about that in my blog post here.

 

Remote Server Administration Tool Mangement of Membership/Roles

 

The built-in Web Administration Tool with Visual Web Developer and VS 2005 makes it easy to manage the users and roles for a local ASP.NET application. One common question I get asked is how to manage these users/roles against a remote server (for example: an application running on a remote hoster.  This blog post of mine points to two different solutions you can use to enable this.

 

How to Share Forms-Authentication Between ASP.NET V1.1 and ASP.NET V2.0 Apps

 

One common question I’ve seen is whether it is possible to share membership and forms-authentication across multiple applications.  The good news is that this is definitely possible.  Even better, it is possible to-do this across V1.1 and V2.0 applications.  This blog post of mine discusses how to-do this.

 

How to encrypt connection strings and web.config file settings

 

ASP.NET 2.0 now allows you to encrypt all configuration settings within the web.config file.  This article walksthrough how to easily do this to secure private data and configuration.

 

Forms Authentication Timeout Change

 

One change between ASP.NET V1.1 and V2.0 was the default timeout value of forms-auth cookies that are issued.  By default out of the box, ASP.NET 2.0 will time-out authentication cookies after 30 minutes of inactivity by the browser user (requiring the user to login on the next visit to the site).  You can learn more about this, and how to change the timeout to your preferred duration setting in my blog post here.

 

Great ASP.NET Security Blog

 

Dominick maintains a great Security blog at: http://www.leastprivilege.com that I'd recommend subscribing to.  He continually posts good information on building more secure apps and on how to take advantage of ASP.NET features.

 

Building Secure ASP.NET Applications Guide

 

The Microsoft PAG (Prescriptive Architecture Guidance) Team has published a great book online about ASP.NET Security Best Practices.  It is focused on ASP.NET V1.0 – but the core concepts still apply. You can read it here.  You can also then find some good tutoral scenarios here.

 

Security Guidelines/Recommendations

ASP.NET 2.0 Security How-To Listing

 

MSDN has a priceless set of ASP.NET Security How-To Articles now published.  Below is a listing with links to some of them:

 

ASP.NET 2.0

·                     How To: Configure the Machine Key in ASP.NET 2.0

·                     How To: Connect to SQL Server Using SQL Authentication in ASP.NET 2.0

·                     How To: Connect to SQL Server Using Windows Authentication in ASP.NET 2.0

·                     How To: Create a Service Account for an ASP.NET 2.0 Application

·                     How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI

·                     How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA

·                     How To: Instrument ASP.NET 2.0 Applications for Security

·                     How To: Improve Security When Hosting Multiple Applications in ASP.NET 2.0

·                     How To: Perform a Security Deployment Review for ASP.NET 2.0

·                     How To: Prevent Cross-Site Scripting in ASP.NET

·                     How To: Protect Forms Authentication in ASP.NET 2.0

·                     How To: Protect From Injection Attacks in ASP.NET

·                     How To: Protect From SQL Injection in ASP.NET

·                     How To: Use ADAM for Roles in ASP.NET 2.0

·                     How To: Use Authorization Manager (AzMan) with ASP.NET 2.0

·                     How To: Use Code Access Security in ASP.NET 2.0

·                     How To: Use Forms Authentication with Active Directory in ASP.NET 2.0

·                     How To: Use Forms Authentication with Active Directory in Multiple Domains in ASP.NET 2.0

·                     How To: Use Forms Authentication with SQL Server in ASP.NET 2.0

·                     How To: Use Health Monitoring in ASP.NET 2.0

·                     How To: Use Impersonation and Delegation in ASP.NET 2.0

·                     How To: Use Medium Trust in ASP.NET 2.0

·                     How To: Use Membership in ASP.NET 2.0

·                     How To: Use the Network Service Account to Access Resources in ASP.NET

·                     How To: Use Protocol Transition and Constrained Delegation in ASP.NET 2.0

·                     How To: Use Regular Expressions to Constrain Input in ASP.NET

·                     How To: Use Role Manager in ASP.NET 2.0

·                     How To: Use Windows Authentication in ASP.NET 2.0

Authentication and Authorization

·                     How To: Connect to SQL Server Using SQL Authentication in ASP.NET 2.0

·                     How To: Connect to SQL Server Using Windows Authentication in ASP.NET 2.0

·                     How To: Create GenericPrincipal Objects with Forms Authentication

·                     How To: Protect Forms Authentication in ASP.NET 2.0

·                     How To: Use Authorization Manager (AzMan) with ASP.NET 2.0

·                     How To: Use Forms Authentication with Active Directory

·                     How To: Use Forms Authentication with Active Directory in ASP.NET 2.0

·                     How To: Use Forms Authentication with Active Directory in Multiple Domains in ASP.NET 2.0

·                     How To: Use Forms Authentication with SQL Server 2000

·                     How To: Use Forms Authentication with SQL Server in ASP.NET 2.0

·                     How To: Use Windows Authentication in ASP.NET 2.0

Code Access Security

·                     How To: Create a Custom Encryption Permission

·                     How To: Use Code Access Security in ASP.NET 2.0

·                     How To: Use Code Access Security Policy to Constrain an Assembly

Code Review

·                     How To: Perform a Security Code Review for Managed Code (Baseline Activity)

Communications Security

·                     How To: Call a Web Service Using Client Certificates from ASP.NET

·                     How To: Call a Web Service Using SSL

·                     How To: Set Up SSL on a Web Server

·                     How To: Set Up Client Certificates

·                     How To: Use IPSec for Filtering Ports and Authentication

·                     How To: Use IPSec to Provide Secure Communication Between Two Servers

·                     How To: Use SSL to Secure Communication with SQL Server 2000

Configuration

·                     How To: Create a Custom Account To Run ASP.NET

·                     How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI

·                     How To: Encrypt Configuration Sections in ASP.NET 2.0 Using RSA

Cryptography

·                     How To: Create a DPAPI Library

·                     How To: Create an Encryption Library

·                     How To: Store an Encrypted Connection String in the Registry

·                     How To: Use DPAPI (Machine Store) from ASP.NET

·                     How To: Use DPAPI (User Store) from ASP.NET with Enterprise Services

Deployment Review

·                     How To: Perform a Security Deployment Review for ASP.NET 2.0

Impersonation and Delegation

·                     How To: Implement Kerberos Delegation for Windows 2000

·                     How To: Use Impersonation and Delegation in ASP.NET 2.0

Input and Data Validation

·                     How To: Prevent Cross-Site Scripting in ASP.NET

·                     How To: Protect From Injection Attacks in ASP.NET

·                     How To: Protect From SQL Injection in ASP.NET

·                     How To: Use Regular Expressions to Constrain Input in ASP.NET

Patching and Updating

·                     How To: Implement Patch Management

SQL Server 2000

·                     How To: Connect to SQL Server Using SQL Authentication in ASP.NET 2.0

·                     How To: Connect to SQL Server Using Windows Authentication in ASP.NET 2.0

·                     How To: Protect From SQL Injection in ASP.NET

·                     How To: Use Forms Authentication with SQL Server in ASP.NET 2.0

·                     How To: Use SSL to Secure Communication with SQL Server 2000

Threat Modeling

·                     How To: Create a Threat Model for a Web Application at Design Time

 

Hope this helps,

 

Scott

Published Friday, February 24, 2006 2:47 AM by ScottGu
Filed under: , ,

Comments

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, February 24, 2006 6:37 AM by Bilal Haidar [MVP]
Hello:
I would like to point out that, I have created a utility few weeks ago, that helps you configure and customize the ASP.NET Provider Template, that is downloadable from the ASP.NET provider ToolKit website.

I have posted about this utility here:
http://bhaidar.net/cs/blog/archive/2006/01/23/55.aspx

Feel free to download this free utility.

Best of Luck,

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, February 24, 2006 8:17 AM by Timo
Yet another excellent post. Lot's of valuable info to go through.

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, February 24, 2006 10:52 AM by Terri Morton
Scott, your continued and dedicated presence at the ASP.NET Forums has made a huge positive impact there. Thanks!! And thank you for your frequent and highly informative blog posts as well. They are really helping a lot of people come up to speed on ASP.NET 2.0.

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, February 24, 2006 11:28 AM by Tarun
I am speech-less,
You are the man.

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, February 24, 2006 1:40 PM by aldone
Hi Scott, you have an excellent blog. It is very useful for me.I hop you continue with this work..

The best wishes for you from Peru :D

Sorry for my poor english.

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, February 24, 2006 7:56 PM by Marc Fairorth
Thanks for your fabulous contributions!

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, February 24, 2006 9:18 PM by Dave Reed
Someone on some other site said they think Microsoft has an entire department named "Scott Gu". I concur!

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, February 24, 2006 10:34 PM by Parag Kantharia
If possible kindly post such detailed blogs on Profiles also. A detailed blog on " TableProfileProvider " is most welcomed.

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, February 24, 2006 11:00 PM by Carlos Azevedo
Hi!

This blog is very good!
Indeed very useful!

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Saturday, February 25, 2006 11:31 AM by David
Our application doesn't fall neatly into the Membership/Roles model because Roles and the Rights assigned to them are user configurable. We looked into extending the Membership model all the way to Membership/Roles/Rights, but we couldn't find enough information. Any pointers.

For example, instead of User.IsInRole("Managers"), we want User.CanDo("EditUsers"), where "EditUsers" can be defined on any one of the Roles that the User is assigned to.

Thanks!

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Saturday, February 25, 2006 11:50 AM by scottgu
Hi David,

Have you looked at using AzMan to define your roles and permissions? I believe it allows the granular sub-task items you are looking for (complete with an admin tool to manage them). This how-to then describes how to call this within ASP.NET 2.0: http://msdn.microsoft.com/library/en-us/dnpag2/html/paght000019.asp?frame=true

Hope this helps,

Scott

P.S. Note that providers can expose their own custom Object Model as well (this can flow through the public APIs). So one approach would be to implement a custom Roles provider that has the granular "CanDo" style checks -- and then use this within your application. Alternatively, you could just use the back-end AzMan provider above.

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Saturday, February 25, 2006 1:50 PM by David
Sweet, Thanks!

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Sunday, February 26, 2006 8:27 AM by Tom Crane
Hi Scott,
Fantastic set of resources, thank you very much!
I’ve already written a couple of custom Membership and Role providers to provide facades for older databases in newer web apps, and found the process pretty painless and sensible. What I'm looking for is guidance on what's sort of the inverse of this process - how best to write new database-driven apps that sit as "team players" in the provider-model world... I haven’t read through all of these resources yet, so there might be a pointer to my questions somewhere in there, but here's what's been going through my head recently:

Suppose I want my customers to be able to build web apps with my "Product X". Maybe Product X is even an instance of a Provider itself; let us call it CustomComplexBusinessLogicProvider or something. I want my customers to be able to use whatever Membership, Role or any other providers they like, so the product can be as flexible in deployment as possible. Some customers might want to use their existing Active Directory so they don't have to manage multiple user sets, others might want a standalone web app that could just use the SqlMembershipProvider.

In an old fashioned monolithic app I’d have a "Users" table in my database, and lots of other tables would have foreign keys for users - any complex app is going to have lots of relationships between users of the system and other entities. But in the provider world, my users and roles live in systems that are unknown to me. I can no longer perform joins across my users and roles tables with the rest of my domain-specific tables.

Do I have a "stub" Users table in my database to act as a bridge, to provide foreign keys to other tables? Does MembershipUser.ProviderUserKey offer a means of establishing identity across providers? This property is typed object - it might be a GUID in one provider and an int in another. I guess a stub table could be like
CREATE TABLE Users ( Id INT IDENTITY(1,1) PRIMARY KEY, ProviderKey CHAR(30) )
Or perhaps the MembershipUser.UserName property would be better as the bridge.

The temptation would creep in to store more and more redundant user details in this table, so that you could produce queries directly in SQL that included user details that are actually stored in the MembershipProvider (SELECT Users.Username, Users.Email, Jobs.Description, [...] FROM Jobs INNER JOIN Users [...] WHERE Jobs.userID=[...]) You'd then need to have a strategy for keeping your Users table in sync with the MembershipProvider.

So, I was wondering whether there are any best practices guidelines out there that answer these sorts of questions - or whether they are even the right questions anyway... In short, what do you watch out for when writing your own CustomComplexBusinessLogicProvider in such a way that your customers can use it with their own choice of other providers?

Tom

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Tuesday, February 28, 2006 2:16 AM by Tom
good

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Tuesday, February 28, 2006 10:59 AM by anjee
Excellent resources, what I want exactly.Thank you very much.

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Thursday, March 02, 2006 4:51 PM by Joel Aemmer
I was wondering if any additional Login Controls existed. I am trying to construct an admin tool similar to the one that comes with VS. I am able to use the existing CreateUser tool, but now need a Modify User and Remove User function. Anywhere I could get pre-build versions of these?
Thanks

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Friday, March 03, 2006 1:51 PM by Ashish Naik
Excellent work. Very nice info in one article!

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Sunday, March 05, 2006 5:55 AM by scottgu
Hi Joel,

I'd recommend checking out this link: http://weblogs.asp.net/scottgu/archive/2006/01/09/434925.aspx It walks through two ways to build an admin tool for the login/membership/roles system and includes source code.

Hope this helps,

Scott

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Monday, March 06, 2006 8:06 AM by nz web guy
Thanks for a great post, theres a lot of goodies in those links!

PS Does anyone know if there is a way to programmatically control the "Remember me next time" expiry time for the ASP.NET login control? It seems to forget a login after a few minutes, yet cookies are allowed. Thanks!

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Monday, March 06, 2006 8:25 AM by iffi
Scott u r simply out-class :)
Keep up the good work buddy ;)

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Monday, March 06, 2006 5:18 PM by scottgu
Hi NZ web guy,

Can you send me an email with more details on the issue you are having? I can then add more people to the thread and we can figure out what is happening.

Thx!

- Scott (scottgu@microsoft.com)

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Monday, March 13, 2006 2:37 AM by Fernando Medina
THANKS, THANKS, THANKS!!!!

# re: ASP.NET 2.0 Membership, Roles, Forms Authentication, and Security Resources

Tuesday, March 14, 2006 1:12 PM by Rodel Dagumampan
hi scott, you're the best ASP.NET blogger. keep it up. Congratualtions from Philippines!

# ASP.NET 2.0 Membership and Roles Tutorial Series

Monday, May 22, 2006 3:28 AM by ScottGu's Blog
Scott Mitchell has continued the great ASP.NET 2.0 Membership, Roles and Profile&amp;nbsp;Tutorial series...

# Always set the &amp;quot;applicationName&amp;quot; property when configuring ASP.NET 2.0 Membership and other Providers

Monday, May 22, 2006 3:36 AM by ScottGu's Blog
I helped out a few folks last night on the ASP.NET Forums with this problem, so I thought it might make...

# Source Code for the Built-in ASP.NET 2.0 Providers Now Available for Download

Monday, May 22, 2006 3:49 AM by ScottGu's Blog
Today we released the source code for the built-in ASP.NET 2.0 Membership, Role Management, Site Navigation,...

# SQL Database Support for ASP.NET Membership, Roles and Personalization

Thursday, May 25, 2006 10:30 AM by Mesfin
I found all the documents here magnificent and i would like to pass my warmest appriciation.
i have one question though.

Here it is
I added the membership table to my sql server and the membership part works fine and lately i use a webparts and i couldn't find the provider for the personalization.
her is my little code

<webParts >
     <personalization  defaultProvider="CustomizedPersonalizationProvider">
       <providers>
         <clear/>
         <add name="CustomizedPersonalizationProvider"
              type="???"              
              connectionStringName="MyDB" />  
       </providers>
     </personalization>
   </webParts>

what do i need to put on the 'TYPE' part,

your help is greatly appricieted