Archive for May, 2011

HTML 5 Offline Cache

1. there are 2 kinds of storage for HTML 5,
window.sessionStorage to retain values for the duration of the session, or
window.localStorage to retain values for longer periods.

2. Example to save and get the value
window.localStorage.setItem(“key”, “value”); // is used to save a variable in local storage
window.sessionStorage.getItem(“key”); // is used to get the variable value

3. If an array is required to save in the data, JSON should be used

// build the object in JSON format
var attendanceEntry = { "StaffName": "Allen Kan", "StaffNo": "222222", "AttendanceType": "In", "AttendanceDateTime": getCurrentDateTime() };

// Since it should be 2d array (an array inside an array), we have to "add" / "push" to the master array
attendanceMaster.push(attendanceEntry)

// the following line to covert object to JSON
JSON.stringify(attendanceMaster)

4. Complete Example

<script type="text/javascript">

// detect current mode is online or offline
function updateIndicator() {
	$('#indicator').html(navigator.onLine ? 'online' : 'offline');
}

// example: save value
function setValue() {

	var attendanceMaster = [];

	if (window.localStorage.getItem("attendanceMaster") != null)
		attendanceMaster = JSON.parse(window.localStorage.getItem("attendanceMaster"));

	// this is hard code entry
	var attendanceEntry = { "StaffName": "Allen Kan", "StaffNo": "222222", "AttendanceType": "In", "AttendanceDateTime": getCurrentDateTime() };
	
	// add the entry to the list
	attendanceMaster.push(attendanceEntry);

	// JSON the string and display it
	buildTable(JSON.stringify(attendanceMaster));

	// set a session value
	window.localStorage.setItem("attendanceMaster", JSON.stringify(attendanceMaster));
}

// example get value
function getValue() {
	// get a session value - returns "my data"
	var attendanceMasterJSON = window.localStorage.getItem("attendanceMaster");
	var attendanceMaster = JSON.parse(attendanceMasterJSON)
	alert(attendanceMaster);

	buildTable(attendanceMasterJSON);
}

// build JSON result to table
function buildTable(tableData) {
	var table = $("#grid");
	table.html("");  //clear out the table if it was previously populated
	eval("var data = " + tableData);  //load the data variable as an object array

	table.append('<thead><tr></tr></thead>');
	var thead = $('thead tr', table);

	//create the table headers
	for (var propertyName in $(data)[0]) {
		thead.append('<th>' + propertyName + '</th>');
	}

	//add the table rows
	$(data).each(function (key, val) {
		table.append('<tr></tr>');
		var tr = $('tr:last', table);
		for (var propertyName in val) {
			tr.append('<td>' + val[propertyName] + '</td>');
		}
	});
}

No Comments

Good Web Downloader

http://www.mipony.net/

No Comments

ASP.Net Membership Authentication and ASPXAUTH Cookie Size Problem

I just ran into the same problem and while I haven’t found a solution, I have found a workaround that may be useful.

The problem is that when the cookie protection mode is set to encryption+validation (which is the default), the resulting cookie is too long. But setting the mode to only protection or only validation, it works fine. So in your web.config file, add the following under :

<authentication mode="Forms">
  <forms protection="Validation"/>
</authentication>

Or use “Encryption” instead of “Validation”.
———————————————————————–

Accordingly to MS, the default validation algorithm is changed in .net 4. Therefore we may defined the same algorithm as below can solve the problem also.

Default Hashing Algorithm Is Now HMACSHA256
ASP.NET uses both encryption and hashing algorithms to help secure data such as forms authentication cookies and view state. By default, ASP.NET 4 now uses the HMACSHA256 algorithm for hash operations on cookies and view state. Earlier versions of ASP.NET used the older HMACSHA1 algorithm.
Your applications might be affected if you run mixed ASP.NET 2.0/ASP.NET 4 environments where data such as forms authentication cookies must work across.NET Framework versions. To configure an ASP.NET 4 Web application to use the older HMACSHA1 algorithm, add the following setting in the Web.config file:

<machineKey validation="SHA1" /> 

No Comments

Setting up FBA Claims in SharePoint 2010 with Active Directory Membership Provider

Setting up FBA Claims in SharePoint 2010 with Active Directory Membership Provider – Sridhar’s blog – Site Home – MSDN Blogs

Ref: http://blogs.msdn.com/b/sridhara/archive/2010/01/07/setting-up-fba-claims-in-sharepoint-2010-with-active-directory-membership-provider.aspx

,

No Comments

Configure MachineKey in ASP.NET 2.0

If we want to (1) share the authentication tickets around applications or (2) web farm web deployment, we have to share the same validationKey and decryptionKey.

Configure MachineKey in ASP.NET 2.0

No Comments

ASP.NET application and page life cycle

ASP.NET application and page life cycle

Ref:

No Comments

Setting Default Button for asp.net Login Control

1. Code Behind Approach

this.Form.DefaultButton = ((CommonButtonControl)LoginUser.FindControl("CommonButtonControl1")).CommoandButton.UniqueID;
// this.Form is the largest "panel" inside

2. Also Code Behind Approach – define the DefaultButton of Panel

void SetDefaultButton(Panel panel, IButtonControl button)
{
	string uniqueId = ((Control)button).UniqueID;
	string panelIdPrefix = panel.NamingContainer.UniqueID + Page.IdSeparator;

	if (uniqueId.StartsWith(panelIdPrefix))
	{
		uniqueId = uniqueId.Substring(panelIdPrefix.Length);
	}

	panel.DefaultButton = uniqueId;
}

3. Define in aspx page

 <asp:Panel ID="panelLogin" runat="server" DefaultButton="Login1$LoginButton">
 <asp:Login ID="Login1" runat="server" >
 <LayoutTemplate>
 ...
 <asp:Button  ID="LoginButton" .../>
 </LayoutTemplate>
 </asp:Login>
 </asp:Panel>

No Comments

Using jQuery to change asp.net DropDownList Content

1. Turn off Event Validation

<%@ Page EnableEventValidation="false"%>

2. Update the javascript (jQuery) event

$.ajax({
	url: "/Services/Authentication.asmx/GetEnrolledCompanies",
	data: "{ 'username': '" + $(userNameId).val() + "' }",
	dataType: "json",
	type: "POST",
	contentType: "application/json; charset=utf-8",
	dataFilter: function (data) { return data; },
	success: function (data) {

		var defaultCompany = "";
		availableTags = eval("(" + data.d + ")");
		$(dropDownlistId).empty(); // Clear existing items

		for (var key in availableTags) {

			// the first one is selected if no default company defined
			if (defaultCompany == "") defaultCompany = key;

			$(dropDownlistId).append($("<option></option>").val(key).html(key));

			if (availableTags[key] == true)
				defaultCompany = key;
		}

		// selected the default company
		alert(defaultCompany);
		$(dropDownlistId + " option[value='" + defaultCompany + "']").attr("selected", "selected");
	},
	error: function (XMLHttpRequest, textStatus, errorThrown) {
		alert(textStatus);
	}
});

3. Use Request.Form and DropDownList1.UniqueID to retrieve the value
*** The DropDownList1.SelectedValue is null once the DropDownList Content is changed by jQuery ***

Request.Form[DropDownList1.UniqueID]

,

No Comments