Run you own web server using PHP / ASP.NET on IIS7 [Part #2 – Install Aquia-Drupal and Wordpress on IIS]

After part #1 about how to make your webserver available publically on the internet, I will discuss how to install php on IIS7 and how to run some known web applications on the same server.
There are 2 ways to install php on IIS, the first and the simplest one is to use the Microsoft’s Web Platform installer, you get the latest stable version of php and fastcgi configured, if you don’t know already, the Web Platform offers dozen of tools for developers, from Visual Studio Express, to database servers, IIS, php, webapps and more…
You can download the web Platform Installer from here 

The second method to install php  detailed here is to download php separately, then add a new script mapping and associate the extension you want to the php handler.

At the end, create a test page on the C:\inetpub\wwwroot directory –test.php for example- and copy this function inside, you have to add the current user to the owners of the directory in order to change or create files in it.

<?php  phpinfo();  ?>


Install WPA certificates (*.cer files) on Android

Installing custom certificates on Android was a real headache and a difficult issue for a lot of people, as for me, I was not able to connect to my university wifi because they are using a custom WPA certificate, you can see details about this here and other related problems with certificates and exchange server here.
The solution that I come over accidently was a small program on the Android Market called wifi helper, it supports :

    1. open wifi
    2. WEP
    3. WPA Personal
    4. WPA2 Personal
    5. WPA Enterprise
    6. WPA2 Enterprise
    7. LEAP
It’s pretty easy to use, and have a lot of advanced features also, I will show here how to use it with an open wifi that requires custom certificate file.
Trying to access the login page for the university wifi website gives the following :
device1 device2







Even selecting continue won’t make your device to connect to this network, so the solution is to download the certificate separately from your computer then upload it to you phone then configure Wifi helper like the following :
Choose Add new configuration –> standard mode then type the network name, in connection mode choose infrastructure, then in the wifi security choose the best that fits your situation, here I use WPA Enterprise, in my case the network wifi is open so I fill what ever in the Identity and Password, then comes the most important part where you choose the certificate file that you downloaded in the SDCARD.
device device3
device4 device5
Now with this set up, you can reconnect to the network and everything will work pretty well not just with the browser but all the apps that access this network.
device6


Using SVN With Visual Studio 2010

SVN is a must have for managing projects and keeping them proper, with a history of changes, what should be done when 2 versions of the same file are changed and so on. It’s so important even for a developer working alone, since code will be never lost.

This post is a quick work through on how to install and use SVN on Windows and Visual Studio, you can read more about SVN here.

First, you should know that having a subversion client on Windows is sufficient, you can do all the stuff in there without the need to have it integrated with Visual Studio, but if you are like me, want perfect handling from within VS then you can go further to integrate it there as well.

We will use TortoiseSVN, a free Subversion client for Windows which integrates a cool shell menu to handle the different SVN commands, install it from here. Once installed you will notice a new shell menus.

1

After that, you have to create a repository (for guys who work on their machines). It is quite simple, you create an empty folder, this folder will be the location where all the history of the projects is saved, so make sure it somewhere safe.

Within this folder, right click and choose TortoiseSVN -> create repository here. Once created, you will notice the following contents added to the folder :

2

Now, you have to choose the repository structure, with whatever structure you want (see the recommendations for that here), right click the content of the directory and choose TortoiseSVN –> import.

With a repository created and with a structure ready, you can start playing with projects from Windows explorer, for example create a Directory and inside it create a text file, then choose SVN checkout, this will add the content to the repository

3

You will notice a little green bullet within the icon of the folder, which means the local content is updated compared to the content of the repository, let’s try something else, create a new text file inside that folder, then right click it and choose TortoiseSVN –> add to repository, this will make a little “+” sign with its icon, which means it’s ready to be committed. At this stage you will notice that the parent folder has changed to a red icon, which means it’s not up to date with the repository anymore.

4 5 7

Right clicking this file, choose SVN commit will bring this screen, where you can add messages to keep history of changes.

6
That’s generally how it works from within windows explorer, after the commit of this file, a new green icon will be back again to the parent folder, of course you can commit the parent folder and all the content will be committed, you can compare differences between local content  and the repository content and so on..
You will find all the commands within the shell menu, and they appear only on folders that you added to the repository, hitting shift + right click will bring more commands

8
Pretty cool, so what about Visual Studio?
Actually you can do the same from the Visual Studio 2010 (or other versions), from the document explorer precisely, there is a free plug-in called AnkhSVN found here

Once installed, it will bring Subversion functionality to Visual Studio, you can verify it’s installed well, and also configure some aspects from Tools –> Options –> Source Control


image

Create a new project, you will notice a little checkbox asking if you want add this project to Subversion or not, then you will get a parameters window where you can configure: the repository to use, the local folder and so on, I’m using a repository through http here, you don’t have to use only local repositories.

9 image

Everything is setup now, on the solution explorer you will notice the “+” signs on the contents, which means they are waiting for a commit to the repository, AnkhSVN bring the same functionality to the project explorer that TortoiseSVN brings to the shell menu (tip: AnkhSVN uses TortoiseSVN repositories, they are not the same), so you will notice the same, red, green icons as well, right clicking a file and choosing SVN –> commit will commit the file to the repository.

10

You can compare files before updating or committing by choosing Subversion –> Compare


11

There are dozens of other functionalities also, you can read more about TortoiseSVN and SVN in general from their website http://tortoisesvn.net/support

Posted in , , |

Windows 7, XP Mode, Compatibility Issues, and MS Haters Feelings

I’ve been using windows 7 since the first day the beta version was out publically, after that I moved to the RC version and now I am using the RTM version. To be honest I am completely admiring it, despite some bugs I’ve noticed on the beta version here and here, and a problem caused by my graphics card here, I still admire it..
Two (2) days ago, I got a genuine 64 bits Windows 7 Professional copy, I installed it yesterday and every thing, including drivers, worked automatically with a little update of the graphics drivers. Despite the fact that it is a 64 bit version, all the programs (most of them are 32 bit) are working normally. Now Windows 7 is my main system after all this period of testing (I erased Vista completely from the HDD).
Note : If you are a student Windows 7 Professional is available for free to the MSDN AA subscribers, if your university / college do not offer MSDN AA account for it’s students you can get Win7 for $30 [link]
This post was aimed to talk about the XP mode, but first I want to mention some reactions from some guys out there claiming that Windows 7 “sucks”, or it is just “a Vista with improved graphics”.
First, someone mentioned that after using Windows 7 for 5 minutes (or 2 hours) he switched back directly to *Linux*, well, no comment, I only say : “take a deep breath, and forget your hurt feelings towards Microsoft for a while, then try Windows 7 again, just admit they are doing great software these days”.
Someone else said : all the programs he run were not compatible with windows 7, I am completely astonished by this, for almost 10 months now, from the beta to the RTM version, I never encountered a software incompatibility on Win7 yet ( the 32 bits or the 64 bits), so in this case, there are two possibilities :

  1. You are running a program from 1703 : if so… ask yourself : do you seriously want to keep running that thing? wouldn’t you consider upgrading it or such? If the answer is no, keep reading, the XP mode might save you.
  2. You never heard about the “Compatibility Mode” introduced years ago, within Win 7 or Vista you can choose which environment the program works (or supposed to work) normally, here is a sample screenshot about that, just right click your program to see this:  1As you see, you can even specify Windows 95, or even the service pack in case of Win XP, 2003 and Vista. Of course Windows 7 is far smarter than this, if it detects that the program is too recent it propose also Windows 2008 and do not allow versions older than XP.
If after all this, your program still doesn’t work (which is not likely to happen) then running XP virtualized programs is your end line solution.

Windows XP mode :

Windows XP mode allows you to run programs within an XP virtualized environment directly from you Windows 7 desktop, I will show my experience installing it and how to quickly make it runs on your computer, although I think I will never need someday.
Windows Virtual PC provides the capability to run multiple Windows environments such as Windows XP Mode from your Windows 7 desktop.
In order to install the Windows XP mode on your computer (which mean get a valid free copy of windows XP virtualized on your pc :) ) you need to have a CPU that supports virtualization, most of recent hardware support that!
Windows Virtual PC requires a CPU with the Intel® Virtualization Technology or AMD-V™ feature turned on. This feature must be enabled in the system BIOS. For details on how to enable, visit the Configure BIOS page or check with your computer manufacturer.
I have an AMD Turion 64x2 CPU, and I used this tool called AMD-V Hyper-V Compatibility Check Utility to test that the CPU is compatible, for Intel CPUs you can use the Intel Processor Identification Utility for this.
Running this utility will show if the CPU you are using support the XP mode or not, and whether the hardware virtualization is enabled in your BIOS or not. For me the CPU is compatible, but virtualization was disabled, so I enabled it in the BIOS as shown bellow :
8
2009-10-16 03.11.58 Once this is done, you run the utility again, and that is what I got :
2
Now you go here http://www.microsoft.com/windows/virtual-pc/download.aspx and download the Virtual PC and the Windows XP mode, be sure to choose the best system type, 32 bits or 64 bits.
After installing it, you should get Windows Virtual PC menu like this in the start menu :
3 Starting the Windows X mode, will bring some wizards to configure the virtualized XP machine, also notice that your HDD will be shared between Windows 7 and the virtualized Windows XP, so that you can easily install programs from there.
4
Now let’s suppose Notepad++ didn’t work on Windows 7, what we have to do is install it inside Windows XP like follows :
5If you turn back to windows 7 start menu now, you can find the newly installed program on windows XP there, you can pin it to the task bar or create a shortcut to the desktop and run it like any other program under windows 7, the window borders will show that it is running on the XP mode:
6And here is the result :
7
As you can see, it is running pretty well like any other program on 7 except that it is on a windows XP, and because it has access to the whole filesystem, you will not notice any difference. Another time, you might never need this someday, this might be handy for companies who *do not* want to upgrade IE 6 for example.
So if after all this, some guys still say “we tried it for 5 mins and no program worked in it” then just know they might have serious psychological problems with the word “Microsoft”.

Posted in , , , |

Prevent JavaScript Injections Using the ASP.NET Built in XSS Filters

XSS is the web vulnerability number 1 according to the OWASP project. If you developed web applications before, you know that its prevention exceeds escaping characters and using some regular expressions to filter out user input.

A good video here, show in 40 minutes XSS and why it’s more dangerous than what most of us think, check it out to understand what XSS is, although it explains only the XSS in the browser and not injected JavaScript like in PDF files or vulnerabilities in QuickTime and other software.

The best way to prevent XSS is to take advantage of the platform you are developing upon. Symphony has a built in functionalities to prevent XSS and CSRF. Today I will show a sneak preview of how the ASP.NET framework protect you from XSS attacks, this functionality has been around years before.

By default trying to inject a script in an ASP.NET form will trigger the exception System.Web.HttpRequestValidationException, as follows :

1 Although this protect you from XSS attacks but it shows your users an ugly exception and even some information that might be sensitive, for example the path of the page that caused the error and such, to solve this issue there are several solutions that I will show 2 among them.

1. Redirect to the default error page using the Web.config file:

As you maybe know already, you can redirect users to a default error page using only declarative syntax in the web.config file, and this includes also exceptions thrown when XSS attacks are encountered.

In the web.config file, insert this code in the <system.web> section:

<customErrors mode ="On" >
<
error statusCode="500" redirect="error_page_handling_500_status.aspx" />
</
customErrors>



For more detailed information about custom errors handling on ASP.NET you can refer here, for example you might want show errors only when connecting to the website remotely etc… You can provide as much error pages as you want mapping the appropriate HTTP error status, in this case we handle the 500 HTTP error code.



2. Override the on errors method and handle manually the response :



This method is more flexible and powerful, since it allows you to handle each exception separately, the only caveat is that you have to send the response HTML to the user yourself, because once an exception is raised, the ASP.NET execution pipeline breaks down. here is a sample of how to do this, in your page inert this code:



protected override void OnError(EventArgs e)
{
if (Server.GetLastError().GetBaseException() is
System.Web.HttpRequestValidationException)
{
Response.Clear();
Response.Write("<html><body>");
Response.Write("<h1> Your input has some unauthorized "
+ "markup, please provide a valid data </h1>");
Response.Write("</body></html>");
Response.StatusCode = 200;
Response.End();

//Response.Redirect("default.aspx");
}
}


Here you can notice that we check if the exception was raised by the detected XSS input, then we clear the response buffer and put some feed back to the user, of course you can send a full HTML page at this stage, and definitely you have to change the HTTP error status to 200 to indicate to the browser (or a JavaScript code using XHR) that the request was executed successfully. 



3 Also notice the commented line, you can simply redirect the user to anther page, but this is not encouraged because users will have no clue about what happened.



Even normal HTML markup is detected as malicious code what to do?



Despite the flexible protection that the ASP.NET XSS filters provides, some times you need to accept such input from your users (<b> or <i> tags…), and hence, you need to tell ASP.NET not to check for XSS anymore. This is not an advised step and in 90% of the cases you will never need to do this.



To specify a page that doesn’t need XSS protection, you add this attribute to the page



ValidateRequest="false"

2 From now on, you have to validate yourself the user input, or even the output of data which is coming from outside sources like web services or a database. One step to never forget is escaping data when rendering it back to the user. This line do it nicely :

Server.HtmlEncode(userData);


Escaping data might sound as an ultimate solution to all the XSS problems, but it is not, most of the cases, programmers do not sanitize completely the data that they write to a database, and when displaying the same data, they consider it trusted, since it resides on their database, and this is when persistent XSS take place. So as an advice: never turn off the XSS validation unless you are 100% sure about what you are doing.



An example of this vulnerability can be found on the BlogEngine 1.3.0.0 open source project (tested here http://www.martani.net/2009/02/windows-7-part-3-internet-explorer-8.html), despite the fact that a brilliant team was working behind it, but… never trust user input.



 




user data is malicious unless proven otherwise.


Build your own retweet / hash bot with #LinqToTwitter

You love twitter? You love Linq too? Then you are gonna adore LinqToTwitter.

LINQ to Twitter is a LINQ Provider for the Twitter micro-blogging service. It uses standard LINQ syntax
for queries and includes method calls for changes via theTwitter API.

LinqToTwitter is one of the most inspiring applications out there made with C# by @JoeMayo. You can download it from here : http://linqtotwitter.codeplex.com/. It is open source and several projects are using it already.

Today I want to show how simple it is to develop your own twitter based application using LinqToTwitter, as for an example we will build a simple retweet / hash bot, like the ones we find on twitter for example hashandroid, hashphp, hashcss and more.

You can use for example such bots to retweet every tweet mentioning your domain name, your own name, your trademark or even your favorite movie. A valuable tool for business too.

Download LinqToTwitter, then start a new Visual Studio solution, we will use C# as the language.

First we need to add a reference to the LinqToTwitter DLL and System.Configuration (Solution Explorer –> References –> Add a Reference).

The code is so simple actually, I will explain as long as we go through the code, you can find the complete source code in the attached file from here.

To be sure our bot won’t retweet tweets that are already processed we will read the ID of the tweet from the configuration file like follows

// Start fetching tweets from the last one we fetched before, to not retweet duplicate tweets
// We do this by searching for tweeting having an ID >= to lastTweetID


saved in the App.config
var lastTweetID = getLastTweetID();


private static string getLastTweetID()
{
return ConfigurationManager.AppSettings["lastTweetID"];
}



Once the last tweet ID retrieved, we fetch all the tweets that have a specified string and are emitted after our lastTweetID:




List<AtomEntry> lstTweets = SearchTwitter(twitterCtx, "martani_net"
, Convert.ToUInt64(lastTweetID));



The function SearchTwitter returns the list of tweets satisfying the criteria,we pass the term to search for and the last tweetID.




private static List<AtomEntry> SearchTwitter(TwitterContext twitterCtx, string searchWrd, ulong lastTweetID)
{
var queryResults =
from search in twitterCtx.Search
where search.Type == SearchType.Search &&
search.Query == searchWrd &&
search.PageSize == 10 &&
search.SinceID == lastTweetID
select search;

foreach (var search in queryResults)
{
return search.Entries.ToList();
}
return null;
}



Now that we have the list of tweets we save the most recent tweet ID so that the next time we fetch only new tweets:




var lastTweet = lstTweets.First();
lastTweetID = lastTweet.ID.Substring(lastTweet.ID.LastIndexOf(':') + 1);

// Save the lastest tweet ID in the App.config file.
saveLastTweetID(lastTweetID);



This is the function that saves the lastTweetID in the app.config file (actually this didn’t work for me!! any help?)




private static void saveLastTweetID(string lastTweetID)
{
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["lastTweetID"].Value = lastTweetID;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}



Everything is ready now, we have just to retweet the new tweets with a little change in their form :




foreach (var entry in lstTweets)
{
//Console.WriteLine(entry.ID);
string via = " (via @" + GetShortName(entry.Author.Name) + ")";
string contentWithoutHTML = DeleteHTML(entry.Content);
string newTweet = contentWithoutHTML.Substring(0,
Math.Min(contentWithoutHTML.Length, 140 - via.Length))
+ via;

// skip tweets that we already retweeted before
if (AlreadyTwittered(contentWithoutHTML))
continue;

twitterCtx.UpdateStatus(newTweet);
//Console.WriteLine(newTweet);
}



Here we are fetching the user name and storing it in the via variable. we use the GetShortName function to get only the user name and not it’s real name. For example entry.Author.Name returns “martani_net (Martani Fakhrou)” so our function returns only “martani_net”




private static string GetShortName(string longName)
{
return longName.Substring(0, longName.IndexOf(' '));
}



Then we get the content of the tweet without any HTML, we use a simple regular expression to delete any html specific tags:




private static string DeleteHTML(string text)
{
Regex reg = new Regex("<[^>]*>");
return reg.Replace(text, "");
}



Then we compose the new tweet which is the content without HTML + the “via (username)” footer. Here we have to be aware that our tweet doesn’t exceed 140 chars which mean the true length of the content can’t exceed 140 – the length of the footer (via @something)



Still one trick to take care of, our retweeted tweets will be fetched also, which means we have to take them away, for this we use the function AlreadyTwittered as follows :




private static bool AlreadyTwittered(string p)
{
// if the tweet ends with ")" and have the string " (via" then
// we might have retweeted it already
// this is a poor cretaria, just for examples here.

if (p.EndsWith(")") && p.IndexOf(" (via") != -1)
return true;
else
return false
;
}



Well this is all, we can now send our new tweet to twitter with the following statement




twitterCtx.UpdateStatus(newTweet);



Of course you have to handle also how this program will execute periodically, each 10 minutes for example.



If you intend to use AOuth then you have to setup your application on twitter to get the secret and API key, otherwise you can use the old authentication system, and yeah LinqToTwitter handle all this for you :).



Download



the source code from here.



I have a lot of tricks to do with LinqToTwitter, and from those, a bot available publically to make users able to set their hashtags or specific words to build their own bot with just few clicks, but I can’t make it to sell an ASP.NET hosting and make my projects real :), any help will be appreciated of course.



Twinq test :



Untitled

Posted in , , , |

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.