Monday, May 19, 2014

Microsoft Anti Cross Site Scripting Library a tool to defend yourself against Cross Site Scripting XSS



To prevent cross site scripting use Microsoft Anti Cross Site Scripting Library its free and need of time following is the link from where you can download.

Microsoft Anti Cross Site Scripting Library

Wednesday, February 12, 2014

Encrypt View State Data for ASP.net Application



Encrypting view state is used to reduce the chance of information disclosure and some one getting information to cause harm to the user.

In Asp.net 2.0 the support for encryption has been enhanced. Now you can define encryption of view state on Page level. Following is the implementation in the Page tag of the aspx file.

<%@ page language="c#" masterpagefile="~/MasterPage.master" inherits="Abc.Default, App_Web_Default.aspx.cdcab7d2" validaterequest="false" theme="ABC" viewstateencryptionmode="Always" enableEventValidation="false" %>

The attribute ViewStateEncryptionMode  has three values.
1) Auto
2) Always
3) Never

The default for ViewStateEncryptionMode is Auto.

We can also set its value in the web.config file as 

<configuration>   
   <system.web>
      <pages ViewStateEncryptionMode="Always" />
   </system.web>

</configuration>

Sunday, January 12, 2014

How to access a radio button selected Boundfield value from a GridView in javascript


function RefreshParent()
{
//get the gridview object
var gv = document.getElementById("grdViewBrand");
//get the gridview input object collection
var rbs = gv.getElementsByTagName("input");
//Traverse the input object collection
for (var i = 0; i < rbs.length; i++)
{
//Check for the checked radio button
if (rbs[i].type == "radio")
{
if (rbs[i].checked)
{
//get the bound field value from the position where check box is checked
// [i+1] is due to gridview dataBoundField includes header
row also which is not present in input collection
var brandName = gv.rows[i+1].cells[0].innerText;
break;
}
}
}

Description

Get the gridview object and its inputs elements.
Traverse through the (input)collection and get the
selected Radio Button row.

Now from the griview object get the rows position and the cell
and call its inner text property

Tuesday, December 31, 2013

Enforce Secure flag for session cookies in ASP.net



To avoid disclosure of sensitive information in transit from the server to the browser,
many applications use HTTP over SSL (HTTPS).
However, because it may be possible to navigate away from the HTTPS protected transport settings of the site,
either by someone specifically providing a link to a non https:// resource or via the application using a absolute reference and mistakenly using http://,
users may subject to their communications being "sniffed" between the browser and server.
Not only is the user data posted to a web server important to protect using HTTPS -
if an attacker were able to see session identifiers passing in plain sight they could reuse
them and masquerade as another user while the session was active (i.e. The user hadn't logged off).
To avoid this from happening, cookies can be set to be "secure" - that is,
they are only to be transmitted when a secure channel is available.

Add the following tag in the webConfig

<System.web>
<httpCookies requireSSL="true"/>
</System.web>

Wednesday, December 11, 2013

Autocomplete to work with Firefox,IE9+ and Chrome ASP.net C# (AutocompleteType does not produce desired results)

AutoCompleteType is not producing the desired results auto complete feature in the password field needs to be disabled which was done through following Asp.net attribute of < asp:TextBox >

< asp:TextBox ID="passcode" runat="server" size="20" TextMode="Password" AutoCompleteType="Disabled" Wrap="False" > < /asp:TextBox >

When it was tested it is not working with IE9 and chrome.
The quick fix for this potential security threat is as below
In the PageLoad event of your aspx.cs file add attribute as follows

passcode.Attributes.Add("autocomplete", "off");

Tuesday, February 12, 2013

WEB Workers (Part 2)

Below is the simple code to see WEB Workers in action

The Main file code
<!DOCTYPE html>
<html>
<body>
<p>Count numbers: <output id="result"></output></p>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>


<script>
var w;
function startWorker()
{
if(typeof(Worker)!=="undefined")
{
if(typeof(w)=="undefined")
{
w=new Worker("demo_workers.js");
}
w.onmessage = function (event) {
document.getElementById("result").innerHTML=event.data;
};
}
else
{
document.getElementById("result").innerHTML="Sorry, your browser does not support Web Workers...";
}
}
function stopWorker()
{
w.terminate();
}
</script>

</body>
</html>

The js file code

function showMessage()
{
postMessage("Hello World.");
}

Sunday, February 10, 2013

Web Workers (Run Locally)

How to run WEB Worker locally with Google Chrome.

IE version 10 supports WEB Workers the previous versions doesnot support.

I face this problem while creating and testing WEB Workers object on my local machine and nothing was happening. In order to run the code on your local machine with Google chrome we have to enable the flag via parameters while running the code


Right click on the browser icon and select properties there a window will appear and you will see Target field. Use following command and append it in the Target field while running Chrome. Please make sure to close all Chrome windows.


  • --allow-file-access-from-files
  • The Target field Look something like following C:\Users\UserNameHere\AppData\Local\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files

Note: this is risky way of running and testing your code.
Please deploy your code on webserver and you dont need to
make these changes.
Please be aware to remove the above changes from browser.

Saturday, February 9, 2013

HTML5 WEB Workers Part 1

Javascript is designed to run in single Thread environment.

Javascript does not support Multi Threading. Javascript is single threaded and this is major hindrance in implementing some cool capabilities in applications.There are other workarounds used by developers but they dont provide the flexibility which is now available with introduction of WEB Workers in HTML5.


WEB Workers are perfect for keeping your UI refresh, performant and responsive for users.

Types of WEB Workers
  • Dedicated Workers
  • Shared Workers
Note: Only Dedicated Workers are discussed in this article

WEB Worker and DOM

Since WEB worker are external files they don't have access to following Javascript objects.
  • The window object
  • The document object
  • The parent object

Continued Part 2

Wednesday, February 6, 2013

CANVAS 2D API (HTML5 CANVAS Part 3)

Drawing on canvas can be done by canvas 2D API
There are plethora of Methods and Properties in canvas 2D API.

2D API reference:click

HTML5 CANVAS Part 2

Canvas Path
To draw a straight line in canvas we use following

  • moveTo(x,y) starting point of the line.
  • lineTo(x,y) end point of the line.
  • stroke() is used to draw the actual line.

The below is the code to draw lines

<canvas ID="canvas_1" width="300px" height="150px" style="border:1px solid;">
Browser Doesnot support HTML5 canvas tag.
</canvas>

<script>
var c= document.getElementById("canvas_1");
var ctx = c.getContext("2d");

//this draw the first line
ctx.moveTo(0,0);
ctx.lineTo(300,150);
ctx.stroke();

//this draw the second line
ctx.moveTo(300,0);
ctx.lineTo(0,150);
ctx.stroke();
</script>
End of Draw Line section

Draw a Circle
To draw a circle we have to use following functions
  • beginPath()set the new context point.
  • arc(x,y,r,start,stop)to define how the circle will be drawn.
  • stroke() draw the circle.

<canvas ID="canvas_circle" width="300px" height="150px" style="border:1px solid;">
Browser Doesnot support HTML5 canvas tag.
</canvas> <script>
var c=document.getElementById("canvas_circle");
var ctx= c.getContext("2d");

//This fill Rectangle Rectangle.

ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,300,150);

//This draws Circle.

ctx.beginPath();
ctx.arc(150,75,70,0,2*Math.PI);
ctx.stroke();
</script>

End of Draw Circle section