Thursday, August 19, 2010

There was an error processing the request

If you get that above error.. and you have no idea what's wrong with it just put change "CustomError='Off'"

Monday, August 16, 2010

WebForm_... is not Defined error

If you get this error "WebForm_ is not defined error..."
and previously you never get this problem...

---
you may install a plugin which compress the .axd file...

---

solution:
you need to exclude them on the compression module...
please check your .axd name.. and exclude them on your HTTP compression module


it needs this 2 file not to be compressed
System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions

and

System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions

Sunday, August 1, 2010

Protect yourself from XSS attack with new ASP 4.0 nuggets

In ASP.NET 4.0, you can replace your usually habit to use <%=%> with this new nuggets <%: %>
This will automatically protect your applications against cross-site script injection (XSS) and HTML injection attacks and avoid duplicate encoding.

So you don't need to worry if you forget to encode your string in the aspx files. or protect it using AntiXSS.

It's very usefull in combination of MVC framework 2.0

Thursday, July 22, 2010

IsCallBack VS IsPostback

I just looking in couple framework. and just curious what they use to bind the UI is using
! IsCallback instead of ! IsPostback

Why ?

Just making summary out of this
http://msdn.microsoft.com/en-us/library/ms178141.aspx


IsCallBack will be set to true if you doing a partial postback.

if you checking using IsCallBack and there is no ajax call , it will not affect anything. Just similar like you don't use the checking which is doesn't improve your performance.

But there should be a reason behind it, or probably they have a mistype because of Autocomplete provided by VS =p

Wednesday, July 14, 2010

Protect your apps from ClickJacking

Here an interesting video which I recently lookat.

http://www.youtube.com/watch?v=gxyLbpldmuU

To protect your apps

put this code

if (top != self)
{
self.location.href = "http://yoursite.com";
}

Monday, July 12, 2010

CSRF Attack Prevention on .NET

In Addition to Rob's AntiXSS, we also need to secure CSRF Attack in Defence Jobs.
Here is what I found :
* Check this video to understand how the CSRF works & How you check your site if it is secure.
* Prevention
- ViewStateUserKey in (ASP.NET)
If you use viewstate in ASP.NET. it is recommended that you include ViewStateUserKey and Encript them
*(Include this on your base page)
protected override OnInit(EventArgs e)
{
base.OnInit(e);
if (User.Identity.IsAuthenticated)
ViewStateUserKey = Session.SessionID;
}
* Encript your viewstate in web.config (ViewStateEncriptionMode="Always")
http://msdn.microsoft.com/en-us/library/aa479501.aspx
Note:
However ViewStateUserKey this is not fully protect you from CSRF. This just to add an addition security layer to your application.
http://keepitlocked.net/archive/2008/05/29/viewstateuserkey-doesn-t-prevent-cross-site-request-forgery.aspx
* Recommended Prevention
Because ViewStateUserKey is not completely protect you from the CSRF Attack, You need to protect your application using per-request nonce to hidden form / URL
There are framework which can automatically done this.
- This .NET version unfortunately only supply protection using URL method. (Nonce token is added on URL). This version doesn't support the hidden field method.
This framework (.NET HTTPModule) will added the per-request nonce to hidden field & cookies and validate it when post method or postback triggered.
========================================================
Installation of ANTICSRF http://anticsrf.codeplex.com/
- Add AntiCSRF.dll to Bin Folder
- Register AntiCSRF HttpModule on web config

....



....
- Configure Settings

....

....

....
 
    


    - If you don't want to proptect your page,
you can add class attribute
[Idunno.AntiCsrf.SuppressCsrfCheck]
or page interface <%@ Implements Interface="Idunno.AntiCsrf.ISuppressCsrfCheck" %>

==================================================================
 

How it works (ANTICSRF)

* HTTP MODULE on PreSendRequestHeaders and PreRequestHandlerExecute

context.PreSendRequestHeaders += PreSendRequestHeaders;

context.PreRequestHandlerExecute += PreRequestHandlerExecute;


* Adding pre-request nonce token
- Add nonce on hidden field __CSRFTOKEN and cokkie __CSRFCOOKIE (configurable on settings)
* Validate nonce token on hidden field with cokkie
- It will validate the token when (POST Request or Postback)

- Get Request will NOT be validated unless it is Postback
- It will NOT validate any SuppressCsrfCheck class attribute or any class which inherits Idunno.AntiCsrf.ISuppressCsrfCheck
* If Attack detected
-When
- hidden field or cookkie token are null/empty
- hidden field and cookkie token is not match
-ACTION (based on configuration setting detectionResult)
-
Throw an exception
- Or redirect to other page based on configuration setting (errorPage)
==================================================================

Limitations of ANTICSRF

  • Non-ASP.NET forms are not protected with this module.
  • ---------------------
    * This Framework will not protect the GET Request (Except if it is postback).
    For example
    - when you use AJAX call using GET Request, It will not validate the token.
    - But if you want to use AJAX call using the POST Request,
    You must Suppress the AntiCSRF validation by what I mention above on the Intallation. by adding the attribute or page attribute.
    Because If you don't suppress the AntiCSRF validation, it will detect as AntiCSRF Attack, because they can't find the token located on your hidden field.
    Please have a read on How It Works explanation above.
    ---------------------

    XSS Prevention attack

    Most web developer must know about RequestValidation configuration in .NET
    which we can disable the XSS attack.

    But If we want to disable 'RequestValidation' it so we can have flexibility to handle it, We can use Server.HtmlEncode(). to display it.

    However this still not enough. This will expose to XSS attack.
    My college (Rob) find a utility which nice to replace Server.HTMLEncode().

    Download AntiXSSLibrary.dll
    and replace Server.HTMLEncode() with AntiXss.UrlEncode();

    http://msdn.microsoft.com/en-us/library/aa973813.aspx
    http://blogs.msdn.com/b/cisg/archive/2008/08/26/what-is-microsoft-antixss.aspx

    Tuesday, June 22, 2010

    Remember clientaccesspolicy

    Remember to have clientaccesspolicy.xml

    to enable your WCF to be consumed by silverlight

    http://videos.visitmix.com/MIX09/T42F
    http://community.dynamics.com/blogs/cesardalatorre/comments/9579.aspx

    Wednesday, June 9, 2010

    Calling SP using NHibernate with IDBCommand

    If there is transaction open.
    you need to supply the transaction by enlist it in NHibernate

    iSession.Transaction.Enlist(sqlComm);

    inject javascript in bookmark

    Get stuff from GBone..

    it's pretty amazing that we can inject javascript to debug on browser

    put this bookmark..
    javascript:
    var b=document.body;
    if(b)
    {
    void(z=document.createElement('script'));
    void(z.src='http://www.company.com/somescript.js');
    void(b.appendChild(z));
    }

    this will run that script on your browser

    Jquery.Data

    Learning new stuff today..from Rob...

    It's better to use Jquery.Data instead of custom attribute in element.
    because it will break some browser for custom attribute which not standard.

    Thanks Rob..

    Here is the example

    jQuery.data(div, "test", { first: 16, last: "pizza!" });
    $("span:first").text(jQuery.data(div, "test").first);
    $("span:last").text(jQuery.data(div, "test").last);

    Thursday, June 3, 2010

    System.Web.Extension conflicting with the GAC

    Be careful when you upgrade the site into 3.5
    if you get this error about ambigous with GAC version.
    then you need to take this out from your bin folder.

    because It may be your version in bin folder is different. 1.1
    but in GAC version is 3.5.

    Wednesday, June 2, 2010

    Without Postback with disable your browser Javascript

    Postback is very heavy code to load (viewstate,etc).
    such as you want to get rid of this from your form .


    So the solution is you can use AJAX.. to handle all this postback to make your page still dynamic or more responsive that before.

    But how about if the client browser disable their javascript..
    Your page will be static.

    so you need to consider this as well.

    So the big picture of this solution is to put ajax as common.
    but on the event onclick or href.. you can't just call that function to call an ajax.

    for example
    href="javascript:CallPostbackFunction();"

    you need to change this to
    href="/page/ajax/somePage.aspx" class="AjaxCall"

    then.. in you can check .. if the browser is enabled the javascript..
    then you can call your ajax function.

    If the javascript is disabled from your browser then you still can load to the proper page. instead of just do nothing.

    //check if browser enabled their javascript
    jQuery(document).ready(function() {
    //perform init and replace to an proper javascript
    var ajaxCalls = $('#ajaxCall');
    ajaxCalls.unbind('click', this.addClick);
    ajaxCalls.bind('click', this.addClick);
    });


    Here a good reference for Object Oriented Programming in Javascript
    http://devedge-temp.mozilla.org/viewsource/2001/oop-javascript/

    Sunday, March 28, 2010

    response.d in .net 3.5 JSON

    Migrate Json .Net 2.0 to .Net 3.5

    Here need to be note...
    ================
    response.d

    While I wish this unexpected change had been more clearly announced, it’s a good one. Here’s how Dave Reed explained it to me:

    {"d": 1 }



    Is not a valid JavaScript statement, where as this:



    [1]



    Is.



    So the wrapping of the "d" parameter prevents direct execution of the string as script. No Object or Array constructor worries.

    [] is JavaScript’s array literal notation, allowing you to instantiate an array without explicitly calling a constructor. To expand on Dave’s explanation, simply consider this code:


    =====================
    make sure you change web.config to use ScriptService of v 3.5 in HttpHandlers

    add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"