Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Authenticating Users in an ASP.NET Application
  2. 2. Web Site Administration Tool <ul><li>From VS 2008, click Website/ ASP.Net Configuration to open Web Site Administration Tool. </li></ul><ul><ul><li>Select Authentication type: </li></ul></ul><ul><ul><ul><li>Windows authentication </li></ul></ul></ul><ul><ul><ul><li>Forms authentication </li></ul></ul></ul><ul><ul><li>Manage users </li></ul></ul><ul><ul><li>Manage roles </li></ul></ul><ul><ul><li>Manage access rules </li></ul></ul>
  3. 3. Authentication via Windows Authentication <ul><li>Select this option if users will access your web site only from a private local network. The site will use built-in Microsoft Windows authentication to identify users. Users with a valid Windows user name and password will be able to access your site. </li></ul><ul><ul><li>Intranet </li></ul></ul>
  4. 4. Forms Authentication <ul><li>Select this option if users will access your web site from the public internet. </li></ul><ul><li>Forms authentication identifies the user by prompting them to enter their credentials through a web form. </li></ul><ul><li>When a user attempts to access an unauthorized resource, they are automatically redirected to the login page where they can enter their credentials. The submitted credentials are then validated against a custom user store - usually a database. </li></ul>
  5. 5. Access Rules <ul><li>A llow or deny access to a particular directory by user name or role. </li></ul><ul><li>Use Web Site Administration Tool to create and manage access rules and it will create an authorization section with Allow or Deny elements in the web.config file for that directory. </li></ul><ul><li>The permissions established for a directory also apply to its subdirectories, unless configuration files in a subdirectory override them. </li></ul><ul><li>Users: </li></ul><ul><ul><li>ALL: Including authenticated and anonymous users. </li></ul></ul><ul><ul><li>Anonymous: Unauthenticated users. </li></ul></ul>
  6. 6. User Accounts and Roles <ul><li>Managing user accounts and roles we can define authorization rules for accessing a particular ASP.NET page or directory for a particular user or role. </li></ul>
  7. 7. How to Create Users and Roles <ul><li>Must start SQLExpress service. </li></ul><ul><ul><li>By default, ASP.Net saves users and roles data in a SQL Server Express file that is stored in App_Data folder. </li></ul></ul><ul><ul><ul><li>file: App_DataASPNETDB.MDF </li></ul></ul></ul><ul><li>From VS 2008, click Website/ASP.Net Configuration to open the Web Site Administration Tool. </li></ul><ul><ul><li>Click Security </li></ul></ul><ul><ul><ul><li>Create User </li></ul></ul></ul><ul><ul><ul><li>Create Role </li></ul></ul></ul><ul><ul><ul><li>Create Access Rules </li></ul></ul></ul>
  8. 8. F orms A uthentication T icket <ul><li>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 (typically) 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. </li></ul>
  9. 9. FormsAuthentication Class <ul><li>System.Web.Security.FormsAuthentication </li></ul><ul><ul><li>Manages forms-authentication services for Web applications. </li></ul></ul><ul><li>Methods: </li></ul><ul><ul><li>RedirectFromLoginPage ( string userName , bool createPersistentCookie ) </li></ul></ul><ul><ul><ul><li>Redirects an authenticated user back to the originally requested URL or the default URL, and write a cookie named ASPAUTH containing an Authentication Ticket. </li></ul></ul></ul><ul><ul><li>RedirectToLoginPage () </li></ul></ul><ul><ul><ul><li>Redirects the browser to the login URL. </li></ul></ul></ul>
  10. 10. Membership Class <ul><li>System.Web.Security.Membership </li></ul><ul><li>ASP.NET membership class gives you a built-in way to validate and store user credentials. </li></ul><ul><ul><li>Including users created by Website Administration Tool and CreateUserWizard. </li></ul></ul><ul><li>Method: </li></ul><ul><ul><li>ValidateUser ( string username , string password ) </li></ul></ul>
  11. 11. Example <ul><li>A website with a public area, such as the home page, a restricted area for members only, and an area for website’s administrator only. </li></ul><ul><ul><li>The restricted area will be a subfolder of the website’s root directory. </li></ul></ul><ul><li>Users: </li></ul><ul><ul><li>Administrator </li></ul></ul><ul><ul><li>Members: Members data are stored in a regular database. </li></ul></ul><ul><ul><ul><li>Example: Sales database’s Users table with UserID, Password and Email fields. </li></ul></ul></ul><ul><ul><li>Anonymous users </li></ul></ul>
  12. 12. <ul><li>Step 1: Create user and role </li></ul><ul><li>Step 2: Create access rules: </li></ul><ul><ul><li>Public area (root directory): Allow All </li></ul></ul><ul><ul><li>Membership only area: </li></ul></ul><ul><ul><ul><li>Rule 1: Allow All </li></ul></ul></ul><ul><ul><ul><li>Rule 2: Deny Anonymous </li></ul></ul></ul><ul><ul><li>Administrator only area: </li></ul></ul><ul><ul><ul><li>Rule 1: Deny All </li></ul></ul></ul><ul><ul><ul><li>Rule 2: Allow administrator </li></ul></ul></ul><ul><li>Step 3: Create Login.Aspx page </li></ul><ul><ul><li>Password textbox: </li></ul></ul><ul><ul><ul><li>TextMode property: password </li></ul></ul></ul>
  13. 13. Code Example: One Login Page to Handle Two Types of Authentication Dim strConn As String = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:salesDB.mdb&quot; Dim objConn As New OleDbConnection(strConn) Dim strSQL, emailAddress As String emailAddress = TextBox1.Text strSQL = &quot;select * from users where UserID= '&quot; & TextBox1.Text & &quot;'&quot; Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() If objDataReader.Read() Then If TextBox2.Text = objDataReader(&quot;password&quot;) Then FormsAuthentication.RedirectFromLoginPage(objDataReader(&quot;UserID&quot;), createPersistentCookie:=False) End If End If If Membership.ValidateUser(TextBox1.Text, TextBox2.Text) = True Then FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, createPersistentCookie:=False) End If
  14. 14. ASP.NET Login Controls <ul><li>The ASP.NET login controls provide a login solution for ASP.NET Web applications without requiring programming. </li></ul><ul><ul><li>By default, these controls use SQLExpress database to manage users. </li></ul></ul><ul><li>Login control </li></ul><ul><li>LoginView control: </li></ul><ul><ul><li>display different information to anonymous and logged-in users. </li></ul></ul><ul><li>PasswordRecovery control </li></ul><ul><li>CreateUserWizard </li></ul><ul><li>ChangePassword control </li></ul>
  15. 15. SQL Injection <ul><li>&quot;SQL Injection&quot; is an unverified/unsanitized user input vulnerability, and the idea is to convince the application to run SQL code that was not intended. </li></ul><ul><li>Exploits applications that use external input for database commands. </li></ul>
  16. 16. SQL Injection Demo <ul><li>On a web page that takes customer ID entered in a textbox as input, then displays the customer’s data. </li></ul><ul><li>1. Retrieve all records: In the textbox, enter: </li></ul><ul><ul><li>‘ OR 1=1 OR CID = ‘ </li></ul></ul><ul><ul><li>2. Guess table name or field name: </li></ul></ul><ul><ul><li>‘ AND 1=(SELECT COUNT(*) FROM Orders ) AND CID=‘ </li></ul></ul><ul><ul><li>3. Finding some users: </li></ul></ul><ul><ul><li>' or cname like 'S%' or cid=‘ </li></ul></ul><ul><ul><li>SQLInjectionDemo </li></ul></ul>
  17. 17. Demo Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strConn As String = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:salesDB.mdb&quot; Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = &quot;select * from customer where cid = '&quot; & TextBox1.Text & &quot;'&quot; Dim objComm As New OleDbCommand(strSQL, objConn) Try objConn.Open() Dim objDataReader As OleDbDataReader objDataReader = objComm.ExecuteReader() GridView1.DataSource = objDataReader GridView1.DataBind() Catch except As SystemException Response.Write(except.Message) End Try End Sub