Wednesday, 7 January 2015

Authentication and Authorization

Authentication

Authentication means validating users. In this step, we verify user credentials to check whether the person trying to log in is the right one or not.

Authorization

Authorization on the other hand is keeping track of what the current user is allowed to see and what should be hidden from him. It is more like keeping a register to what to show and what not to show to the user.
Whenever a user logs in, he will have to authenticate himself with his credentials. Once he is authenticated, he will be authorized to see resources/pages of the website. Mostly these two concepts go together.

Type of Authentications


Windows authentication: In this mode, the users are authenticated on their Windows username and password. This method is least recommended in an internet scenario. In an internet scenario, we should always use "Forms based authentication".
Forms based authentication: In this type of authentication, the user will explicitly have to provide his credentials and these credentials, once verified by the server, will let the user to log in.

Tuesday, 6 January 2015

specify optional settings using Asp.Net Directives in page

ASP.Net directives are instructions to specify optional settings, such as registering a custom control and page language. These settings describe how the web forms (.aspx) or user controls (.ascx) pages are processed by the .Net framework.
The syntax for declaring a directive is:
<%@  directive_name attribute=value  [attribute=value]  %>
In this section, we will just introduce the ASP.Net directives and we will use most of these directives throughout the tutorials.

The Application Directive

The Application directive defines application-specific attributes. It is provided at the top of the global.aspx file.
The basic syntax for a sample Application directive is:
<%@ Application Language="C#" %>
The attributes of the Application directive are:
AttributesDescription
Inheritsthe name of the class from which to inherit
Descriptiontext description of the application. Parsers and compilers ignore this
Languagelanguage used in code blocks

The Assembly Directive

The Assembly directive links an assembly to the page or the application at parse time. This could appear either in the global.asax file for application-wide linking or in the page file or a user control file for linking to a page or user control.
The basic syntax for a sample Assembly directive is:
<%@ Assembly Name ="myassembly" %>
The attributes of the Assembly directive are:
AttributesDescription
Namethe name of the assembly to be linked
Srcthe path to the source file to be linked and compiled dynamically

The Control Directive

The Control directive is used with the user controls and appears in the user control (.ascx) files.
The basic syntax for a sample Control directive is:
<%@ Control Language="C#"  EnableViewState="false" %>
The attributes of the Control directive are:
AttributesDescription
AutoEventWireupthe Boolean value that enables or disables automatic association of events to handlers
ClassNamefile name for the control
Debugthe Boolean value that enables or disables compiling with debug symbols
Descriptiontext description of the control page, ignored by compiler
EnableViewStatethe Boolean value that indicates whether view state is maintained across page requests
Explicitfor VB language, tells the compiler to use Option Explicit mode
Inheritsthe class from which the control page inherits
Languagelanguage for code and script
Srcthe filename for the code-behind class
Strictfor VB language, tells the compiler to use the Option Strict mode

The Implements Directive

The Implement directive indicates that the web page, master page or user control page must implement the specified .Net framework interface.
The basic syntax for an Implements directive is:
<%@ Implements  Interface="interface_name" %>

The Import Directive

The Import directive imports a namespace into a web page, user control pate of application. If the Import directive is specified in the global.asax, then it will apply to the entire application. If it is in a page of user control page, then it would apply to that page or control.
The basic syntax for an Import directive is:
<%@ namespace="System.Drawing" %>

The Master Directive

The Master directive specifies a page file as being the mater page.
The basic syntax for a sample MasterPage directive is:
<%@ MasterPage Language="C#"  AutoEventWireup="true" 
    CodeFile="SiteMater.master.cs" Inherits="SiteMaster"  %>

The MasterType Directive

The MasterType directive assigns a class name to the Master property of a page, to make it strongly typed.
The basic syntax for a MasterType directive is:
<%@ MasterType attribute="value"[attribute="value" ...]  %>

The OutputCache Directive

The OutputCache directive controls the output caching policies of a web page or a user control. We will discuss this directive in details, in data caching.
The basic syntax for a OutputCache directive is:
<%@ OutputCache Duration="15" VaryByParam="None"  %>

The Page Directive

The Page directive defines the attributes specific to the page file for the page parser and the compiler.
The basic syntax for a Page directive is:
<%@ Page Language="C#"  AutoEventWireup="true" 
    CodeFile="Default.aspx.cs" Inherits="_Default"  Trace="true" %>
The attributes of the Page directive are:
AttributesDescription
AutoEventWireupthe Boolean value that enables or disables Page events that are being automatically bound to methods; for example, Page_Load
Bufferthe Boolean value that enables or disables HTTP response buffering
ClassNameclass name for the page
ClientTargetthe browser for which server controls should render content
CodeFilename of the code behind file
Debugthe Boolean value that enables or disables compilation with debug symbols
Descriptiontext description of the page, ignored by the parser
EnableSessionStateenables, disables or makes session state read-only
EnableViewStatethe Boolean value that enables or disables view state across page requests
ErrorPageURL for redirection if an unhandled page exception occurs
Inheritsthe name of the code behind or other class
Languageprogramming language for code
Srcfile name of the code behind class
Traceenables or disables tracing
TraceModeindicates how trace messages are to be displayed - sorted by time or category
Transactionindicates if transactions are supported
ValidateRequestthe Boolean value that indicates whether all input data is validated against a hardcoded list of values

The PreviousPageType Directive

The PreviousPageType directive assigns a class to a page, so that the page is strongly typed.
The basic syntax for a sample PreviousPagetype directive is:
<%@ PreviousPageType attribute="value"[attribute="value" ...]   %>

The Reference Directive

The Reference directive indicates that another page or user control should be compiled and linked to the current page.
The basic syntax for a sample Reference directive is:
<%@ Reference Page ="somepage.aspx" %>

The Register Directive

The Register derivative is used for registering the custom server controls and user controls.
The basic syntax for a sample Register directive is:
<%@ Register Src="~/footer.ascx" TagName="footer" 
    TagPrefix="Tfooter" %>

Controls in ASP.Net

Button Controls:

ASP .Net provides three types of button controls: buttons, link buttons and image buttons. As the names suggest a button displays text within a rectangular area, a link button displays text that looks like a hyperlink. And an Image Button displays an image.

When a user clicks a button control, two events are raised Click and Command.

Basic syntax for button controls:

<asp:Button ID="Button1" runat="server" 
            onclick="Button1_Click" Text="Click" />

Common Properties of the Button control:

PropertyDescription
TextThe text displayed by the button. This is for button and link button controls only.
ImageUrlFor image button control only. The image to be displayed for the button.
AlternateTextFor image button control only. The text to be displayed if the browser can't display the image.
CausesValidationDetermines whether page validation occurs when a user clicks the button. The default is true.
CommandNameA string value that's passed to the Command event when a user clicks the button.
CommandArgumentA string value that's passed to the Command event when a user clicks the button.
PostBackUrlThe URL of the page that should be requested when the user clicks the button.

Text Boxes and Labels:

Text box controls are typically used to accept input from the user. A text box control can accept one or more lines to text depending upon the setting of the TextMode attribute.
Label controls provide an easy way to display text which can be changed from one execution of a page to the next. If you want to display a text that does not change, you use the literal text.
Basic syntax for text controls:
<asp:TextBox ID="txtstate" runat="server" ></asp:TextBox
Common Properties of the Text Box and Labels:
PropertyDescription
TextModeSpecifies the type of text box. SingleLine creates a standard text box, MultiLIne creates a text box that accepts more than one line of text and the Password causes the characters that are entered to be masked. The default is SingleLine.
TextThe text content of the text box
MaxLengthThe maximum number of characters that can be entered into the text box.
WrapIt determines whether or not text wraps automatically for multi-line text box; default is true.
ReadOnlyDetermines whether the user can change the text in the box; default is false, i.e., the user can change the text.
ColumnsThe width of the text box in characters. The actual width is determined based on the font that's used for the text entry
RowsThe height of a multi-line text box in lines. The default value is 0, means a single line text box.
The mostly used attribute for a label control is 'Text', which implies the text displayed on the label.

Check Boxes and Radio Buttons:

A check box displays a single option that the user can either check or uncheck and radio buttons present a group of options from which the user can select just one option.
To create a group of radio buttons, you specify the same name for the GroupName attribute of each radio button in the group. If more than one group is required in a single form specify a different group name for each group.
If you want a check box or radio button to be selected when it's initially displayed, set its Checked attribute to true. If the Checked attribute is set for more than one radio button in a group, then only the last one will be selected.
Basic syntax for check box:
<asp:CheckBox ID= "chkoption" runat= "Server"> 
</asp:CheckBox>
Basic syntax for radio button:
<asp:RadioButton ID= "rdboption" runat= "Server"> 
</asp: RadioButton>
Common Properties of the Check Boxes and Radio Buttons:
PropertyDescription
TextThe text displayed next to the check box or radio button.
CheckedSpecifies whether it is selected or not, default is false.
GroupNameName of the group the control belongs to.

List Controls:

ASP.Net provides the controls: drop-down list, list box, radio button list, check box list and bulleted list. These control let a user choose from one or more items from the list.
List boxes and drop-down list contain one or more list items. These lists could be loaded either by code or by the ListItem Collection Editor.
Basic syntax for list box control:
<asp:ListBox ID="ListBox1" 
      runat="server" 
      AutoPostBack="True" 
      OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
</asp:ListBox>
Basic syntax for a drop-down list control:
<asp:DropDownList ID="DropDownList1" 
     runat="server" 
     AutoPostBack="True" 
     OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>

Common Properties of List box and Drop-down Lists:
PropertyDescription
ItemsThe collection of ListItem objects that represents the items in the control. This property returns an object of type ListItemCollection.
RowsSpecifies the number of items displayed in the box. If actual list contains more rows than displayed then a scroll bar is added.
SelectedIndexThe index of the currently selected item. If more than one item is selected, then the index of the first selected item. If no item is selected, the value of this property is -1.
SelectedValueThe value of the currently selected item. If more than one item is selected, then the value of the first selected item. If no item is selected, the value of this property is an empty string("").
SelectionModeIndicates whether a list box allows single selections or multiple selections.
Common Properties of each list item objects:
PropertyDescription
TextThe text displayed for the item
SelectedIndicates whether the item is selected.
ValueA string value associated with the item.
It is important to notes that:
  • To work with the items in a drop-down list or list box, you use the Items property of the control. This property returns a ListItemCollection object which contains all the items of the list.
  • The SelectedIndexChanged event is raised when the user selects a different item from a drop-down list or list box.

    The List Item Collections:

  • The ListItemCollection object is a collection of ListItem objects. Each ListItem object represents one item in the list. Items in a ListItemCollection are numbered from 0.
    When the items into a list box are loaded using strings like: lstcolor.Items.Add("Blue") . then both the Text and Value properties of the list item are set to the string value you specify. To set it differently you must create a list item object and then add that item to the collection.
    The ListItem Collection Editor is used to add item to a drop-down list or list box. This is used to create a static list of items. To display the Collection Editor select Edit item from the smart tag menu, or select the control and then click the ellipsis button from the Item property in the Properties window.
    Common Properties of List Item Collection:
    PropertyDescription
    Item(integer)A ListItem object that represents the item at the specified index.
    CountThe number of items in the collection.
    Common methods of List Item Collection:
    MethodsDescription
    Add(string)Adds a new item to the end of the collection and assigns the string parameter to the Text property of the item.
    Add(ListItem)Adds a new item to the end of the collection.
    Insert(integer, string)Inserts an item at the specified index location in the collection, and assigns the string parameter to the Text property of the item.
    Insert(integer, ListItem)Inserts the item at the specified index location in the collection.
    Remove(string)Removes the item with the Text value same as the string.
    Remove(ListItem)Removes the specified item.
    RemoveAt(integer)Removes the item at the specified index as the integer.
    ClearRemoves all the items of the collection.
    FindByValue(string)Returns the item whose Value is same as the string.
    FindByValue(Text)Returns the item whose Text is same as the string.

Radio Button list and Check Box list

A radio button list presents a list of mutually exclusive options. A check box list presents a list of independent options. These controls contain a collection of ListItem objects that could be referred to through the Items property of the control.
Basic syntax for radio button list:
<asp:RadioButtonList ID="RadioButtonList1" 
   runat="server"
   AutoPostBack="True" 
   OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
</asp:RadioButtonList>
Basic syntax for check box list:
<asp:CheckBoxList ID="CheckBoxList1" 
   runat="server" 
   AutoPostBack="True" 
   OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>
Common Properties of Check Box and Radio Button Lists:
PropertyDescription
RepeatLayoutThis attribute specifies whether the table tags or the normal html flow to use while formatting the list when it is rendered. The default is Table
RepeatDirectionIt specifies the direction in which the controls to be repeated. The values available are Horizontal and Vertical. Default is Vertical
RepeatColumnsIt specifies the number of columns to use when repeating the controls; default is 0.

Bulleted lists and Numbered lists:

The bulleted list control creates bulleted lists or numbered lists. These controls contain a collection of ListItem objects that could be referred to through the Items property of the control.
Basic syntax of a bulleted list:
<asp:BulletedList ID="BulletedList1" runat="server">
</asp:BulletedList>
Common Properties of the Bulleted List:
PropertyDescription
BulletStyleThis property specifies the style and looks of the bullets, or numbers.
RepeatDirectionIt specifies the direction in which the controls to be repeated. The values available are Horizontal and Vertical. Default is Vertical
RepeatColumnsIt specifies the number of columns to use when repeating the controls; default is 0.

HyperLink Control:

The HyperLink control is like the HTML <a> element.
Basic syntax for a hyperlink control:
<asp:HyperLink ID="HyperLink1" runat="server">
   HyperLink
</asp:HyperLink>
It has the following important properties:
PropertyDescription
ImageUrlPath of the image to be displayed by the control
NavigateUrlTarget link URL
TextThe text to be displayed as the link
TargetThe window or frame which will load the linked page.

Image Control:

The image control is used for displaying images on the web page, or some alternative text, if the image is not available.
Basic syntax for an image control:
<asp:Image ID="Image1" runat="server">
It has the following important properties:
PropertyDescription
AlternateTextAlternate text to be displayed
ImageAlignAlignment options for the control
ImageUrlPath of the image to be displayed by the control