Lesson 2: Using Windows, Forms, Passport, and Anonymous Authentication in .NET

Creator Quick Response Code in .NET Lesson 2: Using Windows, Forms, Passport, and Anonymous Authentication

Lesson 2: Using Windows, Forms, Passport, and Anonymous Authentication
Making QR Code JIS X 0510 In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
www.OnBarcode.com
Barcode Drawer In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
password as a command-line parameter and displaying the hash of the password. The resulting hash can be pasted directly into the Web.config file.
Painting QR Code In Visual C#.NET
Using Barcode generation for Visual Studio .NET Control to generate, create QR Code JIS X 0510 image in .NET framework applications.
www.OnBarcode.com
Creating QR Code 2d Barcode In .NET Framework
Using Barcode generation for .NET framework Control to generate, create Denso QR Bar Code image in .NET framework applications.
www.OnBarcode.com
'VB Imports System.Security.Cryptography Imports System.Text Module Module1 Sub Main(ByVal args As String())
QR Code Printer In VB.NET
Using Barcode encoder for .NET Control to generate, create QR Code 2d barcode image in .NET framework applications.
www.OnBarcode.com
Creating Data Matrix 2d Barcode In VS .NET
Using Barcode creator for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications.
www.OnBarcode.com
Dim myHash As SHA1CryptoServiceProvider = New SHA1CryptoServiceProvider
Barcode Creation In .NET
Using Barcode generator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
www.OnBarcode.com
Generate EAN-13 Supplement 5 In .NET
Using Barcode creator for ASP.NET Control to generate, create GS1 - 13 image in ASP.NET applications.
www.OnBarcode.com
Dim password As Byte() = Encoding.ASCII.GetBytes(args(0))
Matrix Barcode Creator In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create Matrix Barcode image in ASP.NET applications.
www.OnBarcode.com
Code128 Generator In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create Code 128 Code Set C image in ASP.NET applications.
www.OnBarcode.com
myHash.ComputeHash(password)
Bar Code Encoder In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create bar code image in ASP.NET applications.
www.OnBarcode.com
Monarch Encoder In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create Codabar image in ASP.NET applications.
www.OnBarcode.com
For Each thisByte As Byte In myHash.Hash
UPC-A Supplement 2 Creator In None
Using Barcode generation for Office Excel Control to generate, create UCC - 12 image in Office Excel applications.
www.OnBarcode.com
Drawing QR Code In Objective-C
Using Barcode printer for iPad Control to generate, create Quick Response Code image in iPad applications.
www.OnBarcode.com
Console.Write(thisByte.ToString("X2"))
Denso QR Bar Code Printer In None
Using Barcode creator for Microsoft Word Control to generate, create QR Code ISO/IEC18004 image in Word applications.
www.OnBarcode.com
Code 3/9 Scanner In VB.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Next
Barcode Generator In None
Using Barcode encoder for Font Control to generate, create barcode image in Font applications.
www.OnBarcode.com
Encode PDF417 In Visual Studio .NET
Using Barcode maker for .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
Console.WriteLine()
Painting EAN / UCC - 14 In Java
Using Barcode generator for Java Control to generate, create GS1 128 image in Java applications.
www.OnBarcode.com
EAN / UCC - 13 Reader In Visual C#
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
End Sub End Module //C# using System; using System.Security.Cryptography; using System.Text; namespace HashExample { class Program { static void Main(string[] args) { SHA1CryptoServiceProvider myHash=new SHA1CryptoServiceProvider(); byte[] password = Encoding.ASCII.GetBytes(args[0]);
myHash.ComputeHash(password);
foreach (byte thisByte in myHash.Hash) Console.Write(thisByte.ToString("X2")); Console.WriteLine(); } } }
Alternatively, you can call the FormsAuthentication.HashPasswordForStoringInConfigFile method to generate a password hash. This method is described in the next section.
IMPORTANT
Storing credentials in a .config file
You should store credentials in a .config file only during testing. Protecting passwords with a hash is little deterrent to an attacker who can read the contents of the .config file, because hashed pass word databases exist that can quickly identify common passwords.
11
Implementing Authentication and Authorization
The FormsAuthentication Class
The FormsAuthentication class is the basis for all forms authentication in ASP.NET. The class includes the following read-only properties, which you can use to programmat ically examine the current configuration:
FormsCookieName
Returns the configured cookie name used for the current Returns the configured cookie path used for the current appli
application.
FormsCookiePath
cation.
RequireSSL
Gets a value indicating whether the cookie must be transmitted using SSL (that is, over HTTPS only).
Improving security if the Web server has an SSL certificate
IMPORTANT
Enable RequireSSL for best security. This will ensure that forms authentication is encrypted.
SlidingExpiration
Gets a value indicating whether sliding expiration is enabled. Enabling sliding expiration resets the user s authentication timeout with every Web request.
Improving security (at the cost of convenience)
IMPORTANT
Disable SlidingExpiration for the highest level of security. This prevents a session from remain ing open indefinitely.
Additionally, you can call the following methods:
Authenticate
Attempts to validate the credentials against those contained in the configured credential store, given the supplied credentials.
Decrypt
Returns an instance of a FormsAuthenticationTicket class, given a valid encrypted authentication ticket obtained from an HTTP cookie. Produces a string containing an encrypted authentication ticket suit able for use in an HTTP cookie, given a FormsAuthenticationTicket object. Creates an authentication cookie for a given user name. Returns the redirect URL for the original request that caused the redirect to the login page.
Encrypt
GetAuthCookie GetRedirectUrl
HashPasswordForStoringInConfigFile
Given a password and a string identifying the hash type, this routine produces a hash password suitable for storing in a configuration file. If your application stores user credentials in the Web.config
Lesson 2: Using Windows, Forms, Passport, and Anonymous Authentication
file and hashes the password, build this method into a management tool to enable administrators to add users and reset passwords.
RedirectFromLoginPage
Redirects an authenticated user back to the originally requested URL. Call this method after verifying a user s credentials with the Authenticate method. You must pass this method a string and a Boolean value. The string uniquely identifies the user, and the method uses it to generate a cookie based on that information. The Boolean value, if true, allows the browser to use the same cookie across multiple browser sessions. Generally, this unique piece of information should be the user s user name.
RenewTicketIfOld
Conditionally updates the sliding expiration on a FormsAu thenticationTicket object. Creates an authentication ticket and attaches it to the cookie s collection of the outgoing response. It does not perform a redirect. Removes the authentication ticket, essentially logging the user off.
SetAuthCookie
SignOut
Creating a Custom Forms Authentication Page
When using forms authentication, you must include two sections at a minimum:
A forms authentication page A method for users to log off and close their current sessions
To create a forms authentication page, create an ASP.NET Web form to prompt the user for credentials and call members of the System.Web.Security.FormsAuthentication class to authenticate the user and redirect him or her to a protected page. The follow ing code sample demonstrates an overly simple authentication mechanism that just verifies that the contents of usernameTextBox and passwordTextBox are the same, and then calls the RedirectFromLoginPage method to redirect the user to the page originally requested. Notice that the Boolean value passed to RedirectFromLoginPage is true, indi cating that the browser saves the cookie after the browser is closed, enabling the user to remain authenticated if the user closes and reopens his or her browser before the authentication cookie expires.
'VB If usernameTextBox.Text = passwordTextBox.Text Then FormsAuthentication.RedirectFromLoginPage(usernameTextBox.Text, True) End If //C# if (usernameTextBox.Text == passwordTextBox.Text) FormsAuthentication.RedirectFromLoginPage(usernameTextBox.Text, true);
Copyright © OnBarcode.com . All rights reserved.