June 25, 2007, 1:44 a.m.
posted by fractal
Configuring Authentication
To understand security in distributed applications, you must be knowledgeable about the closely intertwined subjects of authentication and authorization. Authentication refers to the process of obtaining credentials from a user and verifying her identity. After an identity has been authenticated, it can be authorized to use various resources. Authorization refers to granting rights based on that identity. ASP.NET provides you with flexible alternatives for authentication. Some, but not all, of these authentication methods also make sense in regular Windows applications. You can perform authentication yourself in code, or delegate authentication to other authorities. Because it's the most comprehensive case, I'll consider authentication in the context of ASP.NET applications first, and discuss other types of distributed applications later in the chapter. No AuthenticationThe simplest form of authentication is no authentication at all. To enable an application to execute without authentication, you add this element to its configuration file: <authentication mode="None" /> Setting the mode to None tells ASP.NET that you don't care about user authentication. The natural consequence of this, of course, is that you can't base authorization on user identities because users are never authenticated. NOTE Configuration Files Each ASP.NET application has its own configuration file named web.config. In addition, there is a computerwide configuration file named machine.config. Sub-webs can have their own independent web.config file as well. Configuration files are XML files that can be dynamically updated at runtime. For more details on the ASP.NET configuration mechanism, refer to MCAD/MCSD Developing and Implementing Web Applications with Visual C# .NET and Visual Studio .NET Training Guide, Exam 70-315 (ISBN 0-7897-2822-2). IIS and ASP.NET AuthenticationOne thing that trips up some developers is that there are actually two separate authentication layers in an ASP.NET application. All requests flow through IIS before they're handed to ASP.NET, and IIS can decide to deny access before the ASP.NET process even knows about the request. Here's a rundown on how the process works:
As you can see, several security authorities interact when the user requests a resource or a Web page. If things aren't behaving the way you think they should, it can be helpful to review this list and make sure that you've considered all the factors involved. Authentication ProvidersSo what happens when a request gets to ASP.NET? The answer depends on the site's configuration. The ASP.NET architecture delegates authentication to an authentication provider—a module whose job it is to verify credentials and provide authentication. ASP.NET ships with three authentication providers:
To select an authentication provider, you make an entry in the <system.web> element in the web.config file for the application. You can use one of these entries to select the corresponding built-in authentication provider: <authentication mode="Windows" /> <authentication mode="Passport" /> <authentication mode="Forms" /> You can also create your own custom authentication provider. This doesn't mean that you plug a new module in place of the supplied provider; it means that you write custom code to perform authentication, and set the authentication mode for the application to None. For example, you might depend on an ISAPI filter to authenticate users at the level of incoming requests. In that case, you wouldn't want to use any of the .NET authentication providers. Configuring IIS AuthenticationIf you decide to use Windows authentication within your applications, you need to consider how to configure IIS authentication because the Windows identities are actually provided by IIS. IIS offers four different authentication methods:
Step-by-Step 11.4 gives you practice in setting authentication at the IIS level.
Passport AuthenticationThe Microsoft .NET Passport is an online service (www.passport.net) that enables users to use a single email address and a password to sign in to any .NET Passport-participating Web site or service. Users can create a free Passport account by registering at any .NET Passport participating Web site or by using the Windows XP .NET Passport Registration Wizard. ASP.NET has built-in connections to Microsoft's Passport authentication service. If your users have signed up with Passport and you configure the authentication mode of the application to be Passport authentication, all authentication duties are offloaded to the Passport servers. Passport uses an encrypted cookie mechanism to indicate authenticated users. If users have already signed in to Passport when they visit your site, they'll be considered authenticated by ASP.NET. Otherwise, they'll be redirected to the Passport servers to log in. To use Passport authentication, you need to download the Passport Software Development Kit (SDK) and install it on your server. The SDK can be found at msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.asp?url=/msdn-files/027/001/885/msdncompositedoc.xml. You also need to license Passport authentication. Currently, the license fees are $10,000 per year, plus a periodic $1,500 testing fee. You can get details on licensing Passport at www.microsoft.com/netservices/passport/. Forms AuthenticationForms authentication provides you with a way to handle authentication using your own custom logic within an ASP.NET application. (Note that this is different from custom authentication using an ISAPI filter, which takes place before the request ever gets to ASP.NET.) With forms authentication, the logic of the application goes like this:
Step-by-Step 11.5 demonstrates the use of forms authentication.
Of course, in a real application you'd likely implement a more sophisticated authentication scheme than just making users select a radio button! But in forms-based authentication, you can use any login scheme you can code. You might, for example, store usernames and IP addresses in a database, and allow only users who connect from their registered IP address. Or you might develop a Web service that allows authenticating users over the Internet. Note the change to the <authorization> element of the configuration file in this example. By default, the <authorization> element contains an <allow> element: <allow users="*" /> With that authorization setting, ASP.NET allows all users—even unauthenticated users—access to application resources. The * wildcard matches any user. For Step-by-Step 11.5, I changed this to a <deny> element: <deny users="?" /> The ? wildcard matches only unauthenticated users. The net effect is to allow authenticated users access to all resources, while denying unauthenticated users access to any resources. The <forms> element contains the name of the URL of the form to use for login authentication, the name of the cookie to use, and a timeout attribute. The timeout attribute controls the period a user can work with the application before being directed back to the login page. (Step-by-Step 11.5 sets to the very low value of 1 minute for testing.) When the user is authenticated, the form calls the RedirectFromLoginPage() method of the FormsAuthentication object. The two parameters to this method are the name of the authenticated user and a Boolean value that controls whether to save a permanent (cross-session) cookie. In Step-by-Step 11.5, the second parameter is false, so the cookie is stored in memory, and only for the length of the browser session. Note that the login form doesn't contain any reference to the page where the user will go after authenticating. The forms authentication provider automatically keeps track of the name of the page that the user was trying to access and sends him there when you call the RedirectFromLoginPage() method. |
- Comment


