Introduction
In this example we will authenticate mobile web users for our application using Forms Authentication.
Other forms of authentication for mobile web applications are:
- Windows Authentication
- Passport Authentication
Technique
When the user requests for a mobile web page from the application and if the user is not authenticated, the user is directed to the login page specified in the configuration settings. The user is prompted to enter the login and password. After the user is authenticated and authorized to access the page, the user is sent to the requested page (or the default page of the web site, if none is specifically requested). The user is now free to access the web site and navigate through the web site without being re-prompted for the login information.
When the user is done with the processing, he/she can Signout from the Application.You can also specify a time-out period for the duration of time after which the validity of the login expires.
Since mobile pages may or may not support cookies, we enable cookieless operation.
Program Details
Step 1: Create the project
Create a Visual C# Mobile Web Application in Visual Studio.Net.
Step 2: Create the login Mobile Web Page
Add a new Mobile Web Form to the project and name the page Login.aspx
Add the controls as shown in figure 1 below
Figure 1: Login.aspx
Change the following properties for controls added on the mobile web page:
Control
Property
Value
Label
Text
Login:
TextBox
Id
txtlogin:
Label
TEXT
Password
Textbox
Id
txtPwd
Password
True
Command
Id
Login
IdD
cmdLogin
Label
Id
lblError
Double click on the Command Control to start adding code to event handler for the onClick Event.
Add the following code to the OnClick event handler of the Command control:
if(FormsAuthentication.Authenticate(txtLogin.Text, txtPwd.Text))
{
FormsAuthentication.SetAuthCookie(txtLogin.Text, false);
MobileFormsAuthentication.RedirectFromLoginPage(txtLogin.Text,true);
}
else
{
lblError.Text = "Please check your credentials";
}
This is the code where the actual authentication occurs. If the user cannot be authenticated in the system, then we display and error message and prevent the user from proceeding in the site.
If the user is authenticated successfully, the user is directed to the requested page.
Add the following line of code to the top of the mobile web page.
using System.Web.Security;
Step 3: Create the other pages in the web site
We can now create the rest of the web site. Since this example is used to demonstrate forms authentication, our site will only contain two mobile web pages. Open the default web form that was created in the project MobileWebForm1.aspx and add a label control, a link control and a Command control. Change the properties of the control as shown in the table below:
Control
Property
Value
Label1
Text
Home Page
Link
Text
Go To Mobile Web Page 2
NavigateURL
MobileWebForm2.aspx
Command
Text
Logout
Id
cmdLogout



Join the conversation! Your thoughts help the community grow.