Authentication in the web.config. Authentication is the process that determines the identity of a user. When you log onto your machine at the start of the day and you are asked for your username and password you are authenticating yourself. Who are you? On the web, once you are authenticated you then have authorization. What are you as a user allowed to see on a particular site. You could log in and your role as a user could be Administrator. Having the role of admin allows you to see files, links, folder and pages that another user won’t be able to see.
In one of my previous post I quickly talked about Authentication and Authorization. Today as my last day of studying some details about the web.config, I am going to revisit these two web.config elements and expand my thoughts on them.
There are three modes options you can have in your web.config within the <authentication> element. Windows, Forms, Passport. To quickly cover these, Windows is more used for the intranet, Forms is more used for standard web pages and passport is Microsoft’s authenticating system. Forms is what we are going to cover today.
Set your web.config up to use forms. This turns on everything you need to start using the membership services.
<system.web>
<authentication mode=”Forms”>
</system.web>
When a user enters a site for the first time he is an anonymous user. Anonymous is the default authentication mode. You can configure your web.config file to redirect the anonymous user to a specific page within the site to become authenticated. After the users is redirected to this login page and once he passes the authentication process he will be issued a cookie.
<system.web>
<authentication mode=”Forms”>
<forms name=”ASPXAUTH”
loginUrl=”login.aspx”
timeout=”30” />
</system.web>
The code above we added a loginUrl attribute which will take all anonymous users to the login.aspx page. Once authenticated the user will be directed to the default.aspx page. The name attribute is the name used for the cookie sent to the end uers. The timeout attribute is the time until the cookie will expire. The time above is set to 30 minutes. These are the basics for the authentication element.
Some other thoughts on the web.config file.
Where is the machine.config file located? Why would I need to know this?
You can find the machine config file C:\WINDOWS\Microsoft.NET\Framework\v2.0.xxxxx\CONFIG
It is important to know where this file is because it houses the default settings for all you applications, not just web applications. I wouldn’t change anything within this file unless you know what you are doing. If you take information from here and paste it within you web.config you will override the machine.config for that particular application. As an example. I don’t like having a password be a minimum of seven characters with at least one nonalphnumeric character included. I include the code below into my web.config file and now I have overridden what the machine.config has set up.
<membership>
<providers>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
</providers>
</membership>