These will be a series articles about security issue of ASP.NET Authentication:
After I wrote the first security article,
Authentication (1): Windows Authentication, I originally planned to write other two articles to summarize Forms
Authentication and Passport Authentication, then concentrate on the
new, cutting edge security OAuth and OpenID Authentication. However, I found out the staff in, such as, Forms Authentication is quite huge, it
is
very hard to summarize this topic within one article (I do not want to
just
make words summary, but with sample for support), then I think I'd
better to split one article into severals with each article only has one
topic. That must be fit to the SOLID principle, especially for
principle one and two:
- The Single-responsibility principle: Every class should have only one responsibility.
- The Open–closed principle: Software entities ... should be open for extension, but closed for modification.
This way will make me easier to write the articles, and also easier for readers to read.
This article will be
A: Forms Authentication using XML
This simple example will demonstrate the Forms Authentication using XML (in fact, web.config here):
Step 1: Create an Empty ASP.NET Web App
We use the current version of Visual Studio 2019 16.9.4 and .NET Framework 4.8 to build the app.
- Start Visual Studio and select Create a new project.
- In the Create a new project dialog, select ASP.NET Web Application (.NET Framework) > Next.
- In the Configure your new project Dialog Box, enter
FormAuth
for Project name
- In the Create a New ASP.NET Web Application Dialog Box, select Empty > Create.
Note
- You have to use empty web app template, otherwise, some logic below will
not work (the regular template associated with a RouteConfig.cs class,
empty template does not have, that class makes the app routing behavior
different) .
- This example originally is from Forms Authentication In ASP.NET, you may see some detailed implementations there.
Step 2: Add three Web Forms: Login, Default, and Welcome- Right Click project > Add > Web Form
- In the opened Box: specify the item name > OK
Login.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="FormAuth.Login" %>
- <!DOCTYPE html>
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <h3>
- Login Page</h3>
- <table>
- <tr>
- <td>
- UserName:</td>
- <td>
- <asp:TextBox ID="UserName" runat="server" />
- </td>
- <td>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
- ControlToValidate="UserName"
- Display="Dynamic"
- ErrorMessage="Cannot be empty."
- runat="server" />
- </td>
- </tr>
- <tr>
- <td>
- Password:</td>
- <td>
- <asp:TextBox ID="UserPass" TextMode="Password"
- runat="server" />
- </td>
- <td>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
- ControlToValidate="UserPass"
- ErrorMessage="Cannot be empty."
- runat="server" />
- </td>
- </tr>
- <tr>
- <td>
- Remember me?</td>
- <td>
- <asp:CheckBox ID="chkboxPersist" runat="server" />
- </td>
- </tr>
- </table>
- <asp:Button ID="Submit1" OnClick="Login_Click" Text="Log In"
- runat="server" />
- <p>
- <asp:Label ID="Msg" ForeColor="red" runat="server" />
- </p>
- </form>
- </body>
- </html>
Login.aspx.cs
- using System;
- using System.Web.Security;
-
- namespace FormAuth
- {
- public partial class Login : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void Login_Click(object sender, EventArgs e)
- {
- if (FormsAuthentication.Authenticate(UserName.Text, UserPass.Text))
- {
- FormsAuthentication.RedirectFromLoginPage(UserName.Text, chkboxPersist.Checked);
- }
- else
- {
- Msg.Text = "Invalid User Name and/or Password";
- }
- }
- }
- }
Defaullt.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FormAuth.Default" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div> Welcomae to Default Page!!!
- </div>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Log Out" />
- </form>
- </body>
- </html>
Default.aspx.cs- using System;
- using System.Web.Security;
-
- namespace FormAuth
- {
- public partial class Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void Button1_Click(object sender, EventArgs e)
- {
- FormsAuthentication.SignOut();
- FormsAuthentication.RedirectToLoginPage();
- }
- }
- }
Welcome.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="FormAuth.Welcome" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div> This is a Welcome Page!!!
- </div>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Log Out" />
- </form>
- </body>
- </html>
Step 3: Configure Forms Authentication
In web.config file,
- <?xml version="1.0" encoding="utf-8"?>
-
- <configuration>
- <appSettings>
- <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
- </appSettings>
- <system.web>
- <compilation debug="true" targetFramework="4.5" />
- <authentication mode="Forms">
- <forms loginUrl="login.aspx" defaultUrl="default.aspx">
- <credentials passwordFormat="Clear">
- <user name="test" password="test"/>
- </credentials>
- </forms>
- </authentication>
- <!--
This section denies access to all files in this application except for
those that you have not explicitly specified by using another setting.
-->
- <authorization>
- <deny users="?"/>
- </authorization>
- <httpRuntime targetFramework="4.5" />
- </system.web>
- <!--
This section gives the unauthenticated user access to the Default1.aspx
page only. It is located in the same folder as this configuration file.
-->
- <location path="welcome.aspx">
- <system.web>
- <authorization>
- <allow users ="*" />
- </authorization>
- </system.web>
- </location>
- <!-- This section gives the unauthenticated user access to all of the files that are stored in the Subdir1 folder. -->
- <location path="subdir1">
- <system.web>
- <authorization>
- <allow users ="*" />
- </authorization>
- </system.web>
- </location>
- </configuration>
What we did,
- In the web.config file:
- Configure Authentication Mode as Froms: <authentication mode="Forms">
- Deny all files: <deny users="?"/>, except one indicated as a login file: login.aspx
- Allow users: <allow users ="*" />, for specific page or location (folder)
- In the login file:
- FormsAuthentication.Authenticate Method, Validates a user name and password against credentials stored in the configuration file for an application.
- In the default page:
- Click the Log Out button, to log out by FormsAuthentication.SignOut();
Run the App the opened page (default) will be redirected to the login page automatically, because access is not allowed,
However,
if we type the address of welcome.aspx page, we can get it without a
login required, because it is accessable without a login protection:
In the login page, type in wrong username/password, or empty, you will get warning:
Type in correct one as defined in web.config file: test/test, you will log in and get:
Click Log Out button, you will log out.
Summary
This article discusses the implementation of Forms Authentication by using XML.
References