Server to client javascript variables, in ASP.Net

Sometimes you happen to want a server side value to be available on the client side, to javascript. But doing so in a clean, modular way is not always trivial. Here’s my solution:
1. Add a hidden input field to the page.
2. Fill that field with values, as many as you’d like -> done with a method on the page.
3. Read those values on the client side -> done with a js function.

1. The hidden field in the aspx page:

<input runat=“server” type=“hidden” id=“Vars” class=“Vars” />

2. The method that adds the variable to the page:

internal void AddHiddenVarValue(string name, string value)

{

    Vars.Attributes[name] = value;

}

3. The js function (assuming that jQuery is in use):

function GetGlobal(varName) {

    return $(“.Vars”).attr(varName);

}

Some notes:
1. I added the class “Vars” to the hidden control because .net changes the id to something like “ctl00_Vars”.
2. Reasons for going for this solution:
A. It’s modular – as long as the “AddHiddenVarValue” method is compatible with the GetGlobal function you can change their behavior and not have to change anything about your global vars/values.
B. It’s easy to debug on the client side – all you have to do is view the page’s source and you can see if the values are as expected.
C. All the variables and their values are concentrated in the same place – very easy to manage and monitor.
D. Best of all – you don’t actually have to use global js vars or inject js code blocks into the page.
3. In this solution I’m adding meaningless attributes to the hidden control (from an HTML point of view). It is possible to encode all these values into the “value” attribute of the hidden control, but I didn’t bother. It’s nice enough for me as it is. 🙂

ASP.Net – Getting a user’s email in a facebook application

Recently facebook enabled facebook applications to request a user’s special permission, which enables the application to obtain the user’s email address (among other things).

I’m using asp.net, and the Facebook Developer Toolkit (FDT) to access facebook’s API. The latest version of the FDT does not support getting the user’s email address via the GetUserInfo method, so I had to find a different way to get the user’s email address.

After a little digging and tinkering, here’s the code I ended up with (code picture, coloring is pretty):

// get an api object
Api api = new Api(new DesktopSession(appKey, fbSessionSecret, fbSigSessionKey, false));

// get the user’s id from facebook via the session key.
// this could be taken from the querystring, but then it is not secured:
// it could be modified by a malicious user
String fbUserId = api.Users.GetLoggedInUser().ToString();

// using a api.SendRequest call to get the user’s info. could not have used
// api.Users.GetInfo() since it doesn’t get the user’s email address
Dictionary<string, string> parameterList = new Dictionary();
parameterList.Add(“method”, “Users.getInfo”);
parameterList.Add(“uids”, fbUserId);
parameterList.Add(“fields”, “email,first_name,last_name,name,pic,sex,current_location,”+
“hometown_location,affiliations,has_added_app,locale,birthday_date”);
parameterList.Add(“session_key”, fbSigSessionKey);
String result = api.SendRequest(parameterList, false);

Note: I’m working with an iframe application, a canvas application might be a little different.

How to give a strong name to a weakly named DLL

“Assembly generation failed Referenced assembly ‘[assembly name]’ does not have a strong name”.

I encountered this error while compiling a DLL with a strong name (so it could be put in the GAC). My DLL had a reference to a weakly named DLL that had to become a strongly named DLL. So how would one go about executing such a task?

Here’s the answer in the form of batch commands:

copy %1.dll %1.dll.backup
call "%VS90COMNTOOLS%vsvars32.bat"
sn.exe -k %1.snk
ildasm /all /out=%1.il %1.dll
ilasm /dll /key=%1.snk %1.il

Note: if the weakly named DLL is named myDll.dll then the execution of the batch file should bewithout the DLL’s extension. That is:
strengthen.bat myDll

Explanation of the batch commands:
1. The copy command backs up the DLL (self explanatory, I hope).
2. vsvars32.bat
3. sn.exe
4. ildasm
5. ilasm

One last note: The variable %VS90COMNTOOLS% might be named %VS80COMNTOOLS% for older Visual Studio versions.

Hello world!

Hello everyone (all two of you), and welcome to my shiny new blog!

The name, quite obviously I hope, is Tom. I’ll be writing about technical issues, hence the name “Tom’s Technical Taunt”. I will probably be dealing mostly with C# and or ASP.Net, but I will not constrict the blog to these topics alone. And yes, I do find it ironic to write a blog about ASP.Net in a system written in PHP. 🙂