22
Website Security ISYS 512

Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Embed Size (px)

Citation preview

Page 1: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Website Security

ISYS 512

Page 2: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Authentication

• Authentication is the process that determines the identity of a user.

Page 3: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Forms Authentication• Use username and password to

authenticate user. • Once the Forms authentication is enabled,

pages cannot be accessed unless the user has the proper authentication. Without authentication, user is redirected to a login page.

• If authenticated, an Authentication Ticket is issued in the form of a cookie and user is redirected back to the requested page.

Page 4: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Forms Authentication Ticket

• After verifying the submitted credentials, a forms authentication ticket is created for the user. This ticket indicates that the user has been authenticated and includes identifying information, such as the username. The forms authentication ticket is stored as a cookie on the client computer. Therefore, subsequent visits to the website include the forms authentication ticket in the HTTP request, thereby enabling the web application to identify the user once they have logged in.

Page 5: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Forms Authentication Flow

User

Authenticated? Login Page

No, redirect to

Website

Yes

Authenticated?

No, redirect to

Yes, write Authentication Ticket as cookie

Yes

Page 6: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Enabling Forms Authentication• Set the authentication mode for the application

by modifying the authentication section in the application root web.config file:

<authentication mode="Forms">

• Deny access to anonymous users by modifying the authentication section in the web.config file:<authorization>

<deny users="?" />

</authorization>

• Create a login page that enables users to enter their usernames and passwords.

• If authenticated, an authorization ticket is issued in the form of a cookie.

Page 7: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Example of Web.configure File

<configuration>

<system.web> <authorization> <deny users="?"/> </authorization> <authentication mode="Forms"> <forms loginUrl="Login.aspx" /> </authentication> </system.web>

</configuration>

Page 8: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

FormsAuthentication Class

• Import system.web.security namespace.• Methods:

– RedirectFromLoginPage(String, boolean)• Redirect user back to the page that sent the user to the login

page, and write a cookie named .ASPXAUTH containing an Authentication Ticket.

– SignOut• Removes the forms-authentication ticket from the browser.

– RedirectToLoginPage()• Redirects the browser to the login URL.

Page 9: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Login Control• Login/Login

• Properties:– UserName– Password

• Event:– Login1_Authenticate

Page 10: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Must Turn Off UnobtrusiveValidationMode:Not Using jQuery

protected void Page_Load(object sender, EventArgs e) { Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None; }

Page 11: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Code Example: User name and password are stored in a database table

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); String strSQL = "select * from users where userID='" + Login1.UserName + "'"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader myReader; myReader = objComm.ExecuteReader(); if (myReader.Read()) { if (Login1.Password == myReader["Password"].ToString()) FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true); else Response.Write("Invalid password, Access denied"); } else Response.Write("User not exist"); objConn.Close(); }

Page 12: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

SignOut Demo• using System.Web.Security;

• A signOut page with a button to SignOut; Then redirect to the home page and trigger the authentication again.

protected void Button1_Click(object sender, EventArgs e) {

FormsAuthentication.SignOut(); FormsAuthentication.RedirectToLoginPage(); }

Page 13: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

SQL Injection Demo

• On a web page that takes customer ID entered in a textbox as input, then displays the customer’s data.

• 1. Retrieve all records:In the textbox, enter:‘ OR 1=1 OR CID = ‘

2. Guess table name or field name:‘ AND 1=(SELECT COUNT(*) FROM Orders) AND CID=‘

3. Finding some users:' or cname like 'S%' or cid=‘

Page 14: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Demo protected void Button1_Click(object sender, EventArgs e) {string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb"; OleDbConnection objConn = new OleDbConnection(strConn); String strSQL = "select * from customer where cid='" + TextBox1.Text + "'"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader myReader; myReader = objComm.ExecuteReader(); if (myReader.HasRows) { GridView1.DataSource = myReader; GridView1.DataBind(); } else Response.Write("User not exist"); objConn.Close(); }

Page 15: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Validation Controls: May need to turn off JQuery

• RequiredFieldValidator:– Control to Validate

• RangeValidator:– MaximumValue, MinimumValue

• CompareValidator:– Control to Validate, Control to compare– Operator such as equal, less than, etc.

• RegularExpressionValidator:– ValidationExpression

• CustomValidator:– ClientValidationFunction;

Page 16: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

What is Regular Expression?

• Regular expression is a language designed to manipulate text. Users use its extensive pattern-matching notations to write regular expressions to:– Search text;– Extract, edit, replace, or delete text substrings;– Validate input data:

• values, formats

• Examples:– *.doc– Select * From Student Where Sname = ‘C%’;

Page 17: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Examples of Regular Expressions

• Allowable values:– San Francisco|Los Angeles|Taipei– A|B|C

• AlphaNumeric– [a-zA-Z0-9]+

• EmpID begins with E followed by 3 digits:– E\d{3}

• String length:– Exactly 3 characters: ^.{3}$

Page 18: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

FileUpload Control

• Properties:– PostedFile:

• This is a System.Web.HttpPostedFile class

• FileName: This name contains the path of the posted file.’

– Contentlength

– ContentType

• Method:– SaveAs – this method save the posted file on server.

Page 19: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Save Uploaded Fileprotected void Button1_Click(object sender, EventArgs e) { string FileName; string strFilePath= "C:\\CSharpExamples\\testASP\\testASP\\Images\\"; FileName = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1); strFilePath = strFilePath + FileName; FileUpload1.SaveAs(strFilePath);

Response.Write("File: " + FileName + " is saved on server"); }

Page 20: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Example of Processing Pictures

• SalesDB database PictureTale:– Picture file name:

• Relative reference• Absolute reference

• Creating links to picture files• Insert pictures in web page

– IMG tag example:<img border="0" src="/images/pulpit.jpg" alt="Pulpit rock"

width="304" height="228">

Page 21: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

This example assumes photos are stored in Images folder

protected void Page_Load(object sender, EventArgs e) { Response.Write("<p align='center'><font size='5'><b>Available Pictures</b></font></p>"); string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\CSharpexamples\\SalesDB2011.accdb";

OleDbConnection objConn = new OleDbConnection(strConn);

string strSQL = "select PicID, PicDescription, PicPath from PictureTable;"; OleDbCommand objComm = new OleDbCommand(strSQL, objConn); objConn.Open(); OleDbDataReader objDataReader; objDataReader = objComm.ExecuteReader(); while (objDataReader.Read()) { Response.Write("<p><img border='0' src='Images/" + objDataReader["PicPath"] + "' width='198' height='151'></p>"); } objConn.Close(); }

Page 22: Website Security ISYS 512. Authentication Authentication is the process that determines the identity of a user

Insurance Claim Example

• Uploading claim pictures for insurance cases.• Each case may have many pictures.• Database:

– CaseTable: CaseID, CaseDate, Agent– CasePics: CaseID, PicPathName

• Each picture is named: CaseID + PictureName and saved in folder: Images

• Create a web page with a dropdown list of CaseID, a File Field control to choose file, and a upload button. The uploaded picture will be saved in the Images folder and a record will be entered in CasePics file.