|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionAs secure information is the key to efficient web programming, web application programmers always have security concerns. This article will explain how to secure your website using ASP.NET Form Authentication. This article assume that the reader is already familiar with ASP.NET programming. Keywords
BackgroundI’ve searched so many sites for a code that I can with the help of it, secure websites from unauthorized access. After searching C# books, I found some nice code that helped me to create this simple application. Hope it can help as a basic architecture. There are three kinds of authentication in ASP.NET:
This article will focus on the first type. Form authentication is cookie based, as ASP.NET places a cookie in the client machine in order to track the user. If the user requests a secure page and has not logged in, then ASP.NET redirects him/her to the login page. Once the user is authenticated, he/she will be allowed to access the requested page. Using the codeIn the web.config, change the mode of authentication to In this section, you will set the default page of the system. The default page is the page that the system will redirect the user to, whenever a fault happens while the user tries to access a secured page. <!-- AUTHENTICATION
This section sets the authentication policies
of the application. Possible modes are "Windows",
"Forms", "Passport" and "None"
"None" No authentication is performed.
"Windows" IIS performs authentication (Basic,
Digest, or Integrated Windows) according to
its settings for the application.
Anonymous access must be disabled in IIS.
"Forms" You provide a custom form (Web page)
for users to enter their credentials, and then
you authenticate them in your application.
A user credential token is stored in a cookie.
"Passport" Authentication is performed via
a centralized authentication service provided
by Microsoft that offers a single logon
and core profile services for member sites.
-->
<authentication mode="Forms">
<forms loginUrl="Login.aspx">
</forms>
</authentication>
This section of the web.config determines the users who will be authorized to or denied from the website. The default value <!-- AUTHORIZATION
This section sets the authorization policies
of the application. You can allow or deny access
to application resources by user or role.
Wildcards: "*" mean everyone, "?" means anonymous
(unauthenticated) users.
-->
<authorization>
<deny users="?" /> <!-- Allow all users -->
<!-- <allow users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
<deny users="[comma separated list of users]"
roles="[comma separated list of roles]"/>
-->
</authorization>
RolesIn some business websites, multiple employees would need access to a system in order to do specific tasks. However, each employee would have a specific role, and specific operations to do, according to the nature of his/her job or security level. E.g., an HR manager might not allowed to view the data of the seals department. ASP.NET provides the concept of roles that gives each role a different view on specific pages. <location path="HRpages">
<system.web>
<authorization>
<allow roles="HR" />
<deny users="*" />
</authorization>
</system.web>
</location>
<location path="salesPages">
<system.web>
<authorization>
<allow roles="sales" />
<deny users="*" />
</authorization>
</system.web>
</location>
Login.aspx.csThis section will show the code that reads the password and the user name from login.aspx and redirects the user to a specific page according to his/her role. private void Submit1_Click (object sender, System.EventArgs e)
{
if(this.TextBox_username.Text.Trim()== "HR_manager"
&& this.TextBox_password.Text.Trim() == "password")
{
// Success, create non-persistent authentication cookie.
FormsAuthentication.SetAuthCookie(
this.TextBox_username.Text.Trim(), flase);
FormsAuthenticationTicket ticket1 =
new FormsAuthenticationTicket(
1, // version
this.TextBox_username.Text.Trim(), // get username from the form
DateTime.Now, // issue time is now
DateTime.Now.AddMinutes(10), // expires in 10 minutes
false, // cookie is not persistent
"HR" // role assignment is stored
// in userData
);
HttpCookie cookie1 = new HttpCookie(
FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(ticket1) );
Response.Cookies.Add(cookie1);
// 4. Do the redirect.
String returnUrl1;
// the login is successful
if (Request.QueryString["ReturnUrl"] == null)
{
returnUrl1 = "HRpages/HR_main.aspx";
}
//login not unsuccessful
else
{
returnUrl1 = Request.QueryString["ReturnUrl"];
}
Response.Redirect(returnUrl1);
}
}
The object One of the
Related tutorials
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||