15 October, 2009

Detect local Server to Server Request in C#.NET

In my current project, I needed to secure file access.

Not any file, but one file that could return any file that users have uploaded to the application!

When Users try to access such an URL, let's say http://myapps/getFile.ashx?idFile=123, I chek the user right to see/access the file. If the user couldn't access the file, I just respond "You don't have access to this file" instead of the file Stream.

For some reason, somewhere else in my app, I need to make an internal WebRequest to a file through getFile.ashx. The problem is I can't get the file because my app is not a granted user for that file.

I need a way for getFile.ashx to know that it's the app itself that make a WebRequest to access the file and thus should be granted.

After some diggin, I found the bool
context.Request.IsLocal
that garantee that the WebRequest is internal.

So getFile either check the user right or evaluate IsLocal and give access if True.

02 October, 2009

LINQ to Javascript - make a Where

I find LINQ very cool for Data querying.

I tried to code a Where function for Javascript Arrays :


Array.prototype.where = function(condition) {
var list = [];
for (var i=0; i < this.length; i++) {
if ( condition.call(this[i]) ) {
list.push(this[i]);
}
}
if ( list.length == 1) {
return list[0];
}
else {
return list;
}
}


You havre to send a "condition" function in the where that will test each element and return an array contained elements that verify the condition.

The context of the condition is the element itself so you can easily access it with "this".

Suppose you have INPUT form elements in your document and you want to retrieve those which id contains "browserWidth".

Here is the code :


$$('input').where(
function() {
return (this.id.indexOf('serviceSelected') != -1);
});


If the return array contains a single element, it returns the element itself.

Access C#.NET Server controls by id on client side

In C#.NET, when you declare a Control runat="server", the id of the Control is not the same on the client side.

For example, a Control named "browserWidth" on the server side will have the following id on the client side : "ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_browserWidth".

Thus, you can't access it with a classic Javascript $("browserWidth"), and it is very difficult to guess what client id the Control will have.

My best hack for that is to use the "class" attribute, that can be multiple, and add a class which name is the same as the id of the Control, like this : class="browserWidth" or CssClass="browserWidth" if you work with an asp.net Control.

This way, I can access my client control with Javascript $$("*.browserWidth")[0].

The only problem comes with the asp:HiddenField Control that doesn't accept CssClass attribute.

On the client side, I have a tricks to access to it :

$$('input').where(function() { return (this.id.indexOf("browserWidth") != -1); })

The "where" fucntion is one of mine explain in the next Post.

This tricks works under two conditions :

- your Control has a unique ID
- the Server ID of the Control is contained in the client ID "ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_browserWidth"

If M$ changes the way client ID are made and decides to not add server control id at the end, that tricks would not work.

22 November, 2007

Action-like URL

In VBScript, in an ASP page, I was looking for a way to declare a querystring parameter without giving it a specific value. In the requested page, I just wanted to check the presence of that particular parameter. My URL looks like this :

http://www.mesconges.fr/comprendre.asp?autoplay

The presence of the autoplay parameter indicate to comprendre.asp to run the first flash video once the page is loaded. Requesting comprendre.asp directly would result in no specific action after the load.

Believe me, it was not easy to test the difference between a querystring parameter that has no value and no parameter at all !
You have to play with :

isObject
isNull
isEmpty
<> ""
and so on...

The best way I found was : Not IsEmpty(request.querystring("autoplay"))

07 November, 2007

Restart Windows Server

Under windows server as under windows since 2000 version, if you don't find the Shutdown button in the start menu, don't worry, you can easily restart whith the MS-DOS console.
Click the start menu, choose "execute...", type "cmd" for opening the command line and type "shutdown -r".
The -r option tells the system to shutdown and then restart. Very usefull if your server is located somewhere in India and you don't want to pay fees for a employee to go pressing physically on the server start button !

18 April, 2007

Instructor for the NATIW

Next week, I am leading a workshop at Nomades Ateliers in Geneva.
The first day will be dedicated to AJAX and the second to the Dojo Toolkit.
Just after that, Sebastien Gruhier will lead a workshop about Ruby on Rails. Next week will 100% Web 2.0 !!
Fell free to read about the workshop and register.

14 February, 2007

workaround to make AJAX calls on Internet Explorer 6

I have found a nice alternative to the traditional try catch method to make AJAX calls work on every browser. Place the following code on top of your Javascript script.
if (!window.XMLHttpRequest) {
window.XMLHttpRequest = function() {
return new ActiveXObject('Microsoft.XMLHTTP');
}
}

This hack defines a function called XMLHttpRequest if XMLHttpRequest doesn't exist natively on the browser.
With this method, you can make AJAX calls with 'new XMLHttpRequest()' even on IE 6 or 5.5.
var ajax_call = new XMLHttpRequest();
...


UPDATE : Thanks to comments on Ajaxian, I added the "window." prefix to the declaration of the XMLHttpRequest function in order to work properly on IE 6 and not redefineing the XHR native object on IE 7.

29 December, 2006

workaround to make firebug console.log function bug free on IE

Firebug is very usefull for web development but its console only works great on Firefox for now.
If you want to keep console.log calls in your app event when your app is used on Firefox without the Firebug plug-in or on IE, you can add this line at the very first start of your JS code :

try { console.log('init console... done'); } catch(e) { console = { log: function() {} } }

This code tries to write some text in the Firebug console. If it fails (no Firebug plug in installed or under IE), it instantiate an object called 'console' that has a method called 'log' that does nothing !

05 December, 2006

BPM Counter

I have writtent a very, very simple tool for counting BPM of a song. All you have to do is to press the button when the bit happens. The value of the button indicates you the Bit Per Minute rate of you beeing a DJ !! Enjoy..

02 October, 2006

AJAX : le guide complet

A collegue and me have written a book about AJAX. It's integrally in french but the tutorials are in Javascript, so you might be able to understand them!

Description of the AJAX book
Buy our AJAX book at the best price!