Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Windows Azure, PHP, ...

About the author

Maarten Balliauw is currently employed as .NET Technical Consultant at RealDolmen. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
View Maarten Balliauw's MVP profile

Search

Latest Twitter

    Follow me on Twitter...

    Archive

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright Maarten Balliauw 2012


    Advanced scenarios with Windows Azure Queues

    For DeveloperFusion, I wrote an article on Windows Azure queues. Interested in working with queues and want to use some advanced techniques? Head over to the article:

    Last week, in Brian Prince’s article, Using the Queuing Service in Windows Azure, you saw how to create, add messages into, retrieve and consume those messages from Windows Azure Queues. While being a simple, easy-to-use mechanism, a lot of scenarios are possible using this near-FIFO queuing mechanism. In this article we are going to focus on three scenarios which show how queues can be an important and extremely scalable component in any application architecture:

    • Back off polling, a method to lessen the number of transactions in your queue and therefore reduce the bandwidth used.
    • Patterns for working with large queue messages, a method to overcome the maximal size for a queue message and support a greater amount of data.
    • Using a queue as a state machine.

    The techniques used in every scenario can be re-used in many applications and often be combined into an approach that is both scalable and reliable.

    To get started, you will need to install the Windows Azure Tools for Microsoft Visual Studio. The current version is 1.4, and that is the version we will be using. You can download it from http://www.microsoft.com/windowsazure/sdk/.

    N.B. The code that accompanies this article comes as a single Visual Studio solution with three console application projects in it, one for each scenario covered. To keep code samples short and to the point, the article covers only the code that polls the queue and not the additional code that populates it. Readers are encouraged to discover this themselves.

    Want more content? Check Advanced scenarios with Windows Azure Queues. Enjoy!


    Categories: Azure | C# | General | ICT | Publications

    Wordpress auto sign-on with IIS7 and a plugin

    For our RealDolmen blog platform, where we use Wordpress as the engine running multiple external and internal blogs (yes, that’s an internal SaaS we have there!), we wanted to have an easy solution for our employees to sign-on to the platform. We had a look at the Wordpress plugin repository and found the excellent Simple LDAP Login plugin for providing sign-on through Active Directory. This allowed for sign-on using Active Directory credentials. However, when browsing the blogs from the corporate network, the login page is one extra step in the way of users: they are already logged on to the network, so why sign-on again using the same credentials?

    Luckily for us, we are hosting Wordpress on Windows, IIS 7 and SQL Server. Shocked? No Linux, MySQL, .htaccess and mod_rewrite there! And it works perfectly. In fact, we get some extras for free: single sign-on is made possible by IIS!

    Configuring Windows Authentication in IIS7

    In order to provide a single sign-on scenario for Wordpress on IIS, simply enable Windows Authentication in the IIS7 management console, like so:

    Windows Authentication in IIS - Wordpress, PHP

    If you now browse to the Wordpress site… Nothing happens! Except the normal stuff: a non-logged-in version of the site is displaying… The reason for this is obvious: anonymous authentication is also enabled and is higher up the chain, hence IIS7 refuses to authenticate the user using his Active Directory credentials… One solution may be to reverse the order, but that would mean *every* single user is required to sign-on. Not the ideal situation… And that’s where our custom plugin for Wordpress comes in handy, heck, we’re even sharing it with you so you can use it too!

    Fooling IIS7 when required…

    A solution to the fact that anonymous authentication is higher up the chain in IIS7 and that this is required by the fact that we don’t want everyone to have to login, is fooling IIS7 into believing that Windows Authentication is higher up the chain in some situations… And why not do that from PHP and wrap that “hack” into a Wordpress plugin?

    The basis for our plugin is the following: whenever a user browses the website and uses Internet Explorer (sorry, no support for this in the other browsers…), Windows Authentication is a possibility. The only step left is triggering this, which is pretty easy: if you detect a user is coming from the local LAN and is using Internet Explorer (on Windows), send the user a HTTP/1.1 401 Unauthorized header. This will make IE send out the Windows Authentication token to the server and will also trick IIS7 into thinking that anonymous authentication failed, which will immediately trigger Windows Authentication server-side as well.

    Now how to do this in a Wordpress plugin? Well, simple: hook into 2 events Wordpress offers, namely init and login_form. Init? Well, yes! You want users to automatically sign-on when coming from the LAN. There’s no better hook to do that than init. The other one is obvious: if a user somehow lands at the login page and is coming from the local LAN, you want that page to be skipped and use Windows Authentication there. Here’s some simplified code for registering the hooks:

    1 <?php 2 add_action('init', 'iisauth_auto_login'); 3 add_action('login_form', 'iisauth_wp_login_form');

    Next, implementation! Let’s start with what happens on init:

    1 function iisauth_auto_login() { 2 if (!is_user_logged_in() && iisauth_is_lan_user() && iisauth_using_ie()) { 3 iisauth_wp_login_form(); 4 } 5 }

    As you can see: whenever we suspect a user is coming from the internal LAN and is using IE, we call the iisauth_wp_login_form() method (which “by accident” also gets triggered when a user is on the login page). Here’s that code:

    1 function iisauth_wp_login_form() { 2 // Checks if IIS provided a user, and if not, rejects the request with 401 3 // so that it can be authenticated 4 if (iisauth_is_lan_user() && iisauth_using_ie() && empty($_SERVER["REMOTE_USER"])) { 5 nocache_headers(); 6 header("HTTP/1.1 401 Unauthorized"); 7 ob_clean(); 8 exit(); 9 } else if (iisauth_is_lan_user() && iisauth_using_ie() && !empty($_SERVER["REMOTE_USER"])) { 10 if (function_exists('get_userdatabylogin')) { 11 $username = strtolower(substr($_SERVER['REMOTE_USER'], strrpos($_SERVER['REMOTE_USER'], '\\') + 1)); 12 13 $user = get_userdatabylogin($username); 14 if (!is_a($user, 'WP_User')) { 15 // Create the user 16 $newUserId = iisauth_create_wp_user($username); 17 if (!is_a($newUserId, 'WP_Error')) { 18 $user = get_userdatabylogin($username); 19 } 20 } 21 22 if ($user && $username == $user->user_login) { 23 // Clean buffers 24 ob_clean(); 25 26 // Feed WordPress a double-MD5 hash (MD5 of value generated in check_passwords) 27 $password = md5($user->user_pass); 28 29 // User is now authorized; force WordPress to use the generated password 30 $using_cookie = true; 31 wp_setcookie($user->user_login, $password, $using_cookie); 32 33 // Redirect and stop execution 34 $redirectUrl = home_url(); 35 if (isset($_GET['redirect_to'])) { 36 $redirectUrl = $_GET['redirect_to']; 37 } 38 wp_redirect($redirectUrl); 39 exit; 40 } 41 } 42 } 43 }

    What happens here is that the authentication header is sent when needed, and once a user is provided by IIS we just log the user in to Wordpress and redirect him. The real “magic” is in this part:

    1 // Checks if IIS provided a user, and if not, rejects the request with 401 2 // so that it can be authenticated 3 if (iisauth_is_lan_user() && iisauth_using_ie() && empty($_SERVER["REMOTE_USER"])) { 4 nocache_headers(); 5 header("HTTP/1.1 401 Unauthorized"); 6 ob_clean(); 7 exit(); 8 }

    Which does exactly what I described before in this post…

    Download

    Well of course, feel free to use this plugin! Here’s the source code: iisauth.zip (1.44 kb)

    (And big thanks to our marketing manager for allowing me to distribute this little plugin! Again proof for the no-nonsense spirit at RealDolmen!)


    Writing for the Windows Azure for PHP portal

    image

    I actually just noticed it has been a while since I did a blog post. I also know that writing about this is not really a good idea in the blogosphere. Unless… it’s for a good reason!

    The good reason for not being that active on my blog lately is the fact that I’m producing content for Microsoft’s Interoperability team. Have you ever wanted to start working with Windows Azure and PHP? No idea where to start? Meet the official portal: Developing Applications for Azure with PHP.

    I’ve currently posted some tutorials and scenarios out there, but there’s more to come. Here’s a list of what’s currently available:

    So whenever you think I’m relaxing and doing nothing, check http://azurephp.interoperabilitybridges.com for new content. By the way, if you are doing PHP and Azure, drop me a line. It’s always good to know and maybe I can be of help.

    Stay tuned for more on this!


    Categories: Azure | General | ICT | PHP | Publications

    Cost Architecting for Windows Azure

    Cost architecting for Windows AzureJust wanted to do a quick plug to an article I’ve written for TechNet Magazine: Windows Azure: Cost Architecting for Windows Azure.

    Designing applications and solutions for cloud computing and Windows Azure requires a completely different way of considering the operating costs.

    Cloud computing and platforms like Windows Azure are billed as “the next big thing” in IT. This certainly seems true when you consider the myriad advantages to cloud computing.

    Computing and storage become an on-demand story that you can use at any time, paying only for what you effectively use. However, this also poses a problem. If a cloud application is designed like a regular application, chances are that that application’s cost perspective will not be as expected.

    Want to read more? Check the full article. I will also be doing a session on this later this month for the Belgian Azure User Group.


    Taking Care of a Cloud Environment (slides)

    It looks like I’m only doing sessions lately :-) Here’s another slide deck for a presentation I did on the Architect Forum last week in Belgium.

    Abstract: “No, this session is not about greener IT. Learn about using the RoleEnvironment and diagnostics provided by Windows Azure. Communication between roles, logging and automatic upscaling of your application are just some of the possibilities of what you can do if you know about how the Windows Azure environment works.”

    Thanks for attending!


    Just Another Wordpress Weblog, But More Cloudy

    4322759659_6cab114506_b Slides of my talk at the PHPBenelux conference last weekend are online. Bit of a pity my live demo went wrong due to my www.azure.com trial account going into read-only mode while doing the demo.

    Abstract: "While working together with Microsoft on the Windows Azure SDK for PHP, we found that we needed an popular example application hosted on Microsoft’s Windows Azure. Wordpress was an obvious choice, but not an obvious task. Learn more about Windows Azure, the PHP SDK that we developed, SQL Azure and about the problems we faced porting an existing PHP application to Windows Azure."

    Thanks for joining the conference and my session! And thanks to the PHPBenelux crew for organizing their first conference ever, it rocked!


    MSDN session on ASP.NET MVC

    As promised to all people attending my online session on ASP.NET MVC this afternoon, here is the slide deck I’ve used.

    I must say, doing a presentation using Live Meeting and a Microsoft Roundtable device seemed a bit strange at first. However, the setup that is used to do this kind of sessions is really cool to work with! Make sure to check Katrien’s blog for all other Live Meeting MSDN sessions that are planned.

    kick it on DotNetKicks.com


    Speaking at DevDays 2009, The Hague (and more)

    DevDays09, The NetherlandsSome Great news (at least: I think this is great :-)): Kevin Dockx and I will be giving a session on PHP and Silverlight at the Netherlands DevDays09. This event, aimed totally at developers, will take place on May 28 and 29th in The Hague (Den Haag). It’s the first time the DevDays are hosting sessions related to PHP, and it’s also the first time I’ll be speaking at an event of this size.

    Our session will cover the basics of Silverlight and show you how you can create rich web applications using the best of 2 worlds: Silverlight and PHP.

    dpc09_speakerNext to this event, we’ll be doing this very same session at the Dutch PHP Conference in Amsterdam, which will take place June 11 – June 13th. Yes, we are evil, doing a presentation on Microsoft technologies at a PHP event.

    And now that I’m announcing upcoming sessions anyway… I’ll be doing a session on mocking at VISUG (Visual Studio User Group Belgium). Come visit us at May 7th! This session will show you the basics of unit testing without having to rely on external dependencies like databases, WCF services, ... and will show you how you can reproduce behaviour in a unit test that might be otherwide difficult to test.

    Oh, and another one: MSDN Belgium is hosting some virtual events. I’ll be doing a session on the ASP.NET MVC framework (in Dutch) on April 23 at 14:00. Check the MSDN pages for more info (yet to come). Here’s the abstract of that session:

    "Join us for this 90 min session and get a developer-focused overview on ASP.NET MVC (Model-View-Controller), a new approach in building ASP.NET applications. Learn how the new ASP.NET MVC framework differs and can be an alternative to the current ASP.NET Web Forms framework. This session will help you to learn how to take advantage of ASP.NET MVC to build loosely coupled, highly testable, agile applications. You will see how ASP.NET MVC provides you with fine-grained control over both HTML and JavaScript."

    Sample chapter from ASP.NET MVC 1.0 Quickly

    image Here’s a shameless, commercial blogpost… With yesterday’s 1.0 release of the ASP.NET MVC framework, I’m sure the following sample chapter from my book ASP.NET MVC 1.0 Quickly will be of use for people starting ASP.NET MVC development: Your first ASP.NET MVC application.

    When downloading and installing the ASP.NET MVC framework SDK, a new project template is installed in Visual Studio. This chapter describes how to use the ASP.NET MVC project template that is installed in Visual Studio. All ASP.NET MVC aspects are touched briefly by creating a new ASP.NET MVC web application based on this Visual Studio template. Besides view, controller, and model, new concepts including ViewData—a means of transferring data between controller and view, routing—the link between a web browser URL and a specific action method inside a controller, and unit testing of a controller are also illustrated here.

    In this chapter, you will:

    • Have an overview of all the aspects of an ASP.NET MVC web application
    • Explore the ASP.NET MVC web application project template that is installed in Visual Studio 2008
    • Create a first action method and corresponding view
    • Create a strong-typed view
    • Learn how a controller action method can pass strong-typed ViewData to the view
    • Learn what unit testing is all about, and why it should be used
    • Learn how to create a unit test for an action method by using Visual Studio's unit test generation wizard and modifying the unit test code by hand

    Download the free sample chapter here. Or order the full book, here. That’s a better option ;-)

    By the way, if you are interested in the book writing process itself, check my previous blog post on that.

    kick it on DotNetKicks.com


    Categories: ASP.NET | Books | C# | General | MVC | Personal | Publications

    Announcing my book: ASP.NET MVC 1.0 Quickly

    ASP.NET MVC 1.0 Quickly It’s been quite a job, but there it is: Packt just announced my very first book on their site. It is titled “ASP.NET MVC 1.0 Quickly”, covering all aspects ASP.NET MVC offers in a to-the-point manner with hands-on examples. The book walks through the main concepts of the MVC framework to help existing ASP.NET developers to move on to a higher level. It includes clear instructions and lots of code examples. It takes a simple approach, thereby allowing you to work with all facets of web application development. Some keywords: Model-view-controller, ASP.NET MVC architecture and components, unit testing, mocking, AJAX using MS Ajax and jQuery, reference application and resources.

    That’s it for the marketing part: let’s do a retrospective on the writing process itself. Oh and yes, those are my glasses on the cover. Photo was taken on the beach near Bray-Dunes (France).

    When did you have the idea of writing a book?

    I'm not sure about that. I've been blogging a lot on ASP.NET MVC last year, wrote an article for .NET magazine, did some presentations, ... It occurred to me that I had a lot of material which I could bundle. Together with that, my project manager jokingly said something like: "When will you write your first book? With all that blogging." So I did start bundling stories. First of all, I overlooked the whole ASP.NET MVC technology (preview 2 at that moment) and decided there were enough topics to talk about. A draft table of contents was built quite quick, but I gave up on writing. Too much information, not enough time, ...

    A few weeks later, it must have been around the beginning of May, 2008, I did start writing a first chapter, thinking I'ld see how the writing itself would turn out, if it fit in my schedule, ... It worked out quite well, each 10-20 days gave me a new chapter. I also started looking for a publisher when I was finished with chapter 6 or so. Having reviewed some books for Packt, I contacted them with a proposal for my book.

    After having a look at the other 6 upcoming books (here and here), we decided we could go for it, focusing on a hands-on book which rapidly guides you into the wonderful world of ASP.NET MVC.

    How was your experience of writing your book?

    Looking back, it was an interesting experience. I decided to write in English, which is not my native language. That was actually quite a hard one: writing in English is no problem, but writing a good, solid and interesting piece of text is just not that easy when writing longer texts than the average blog post. Another thing is that I tortured myself writing about a product that was not even beta yet! I started writing with ASP.NET MVC preview 3, updated it all to preview 4, 5, beta, release candidate, ... Lots of changes in the ASP.NET MVC API or concepts meant lots of changes to make in chapters I already wrote. Luckily, I survived :-)

    I only contacted a publisher when I had finished 60% of my book. If you are considering writing: don't do this! Contact a publisher at a very early stage: they normally give you lots of advice upfront, which I only received after contacting them. Advice earlier along the way is always better, so that's something I would definately do different.

    Speaking of advice: when writing was done, the book entered review phase. Different people received the draft version and could provide comments and suggestions. Thanks Stefan, Troy, Vivek, Jerry, Joydip and people at Packt for your time in reviewing my draft version! Reviewer comments really made the book better and required me to do some small rewrites, elaborate more on certain topics.

    What tools did you use for writing?

    There are some tools that you really need when writing a technical book. One of them is a text editor, in my case Microsoft Word 2007. Together with that, Visual Studio 2008 and regularly updated ASP.NET MVC versions were required. Being scared of losing data, I decided to also use a source control system for sample code ánd for my Word documents. All of these files were stored in a Subversion repository located on my server, being backed up every day to different locations. Doug Mahugh laughed at me when I said I was using Subversion, but it did a great job!

    Other tools I used were Paint.NET and MwSnap, both for creating screenshots in my virtual PC running Windows Vista and Visual Studio 2008. I also used Achievo for time tracking purposes, since I was curious how much time this book writing would actually cost me.

    How much time did you spend writing?

    First of all, this is not going to be 100% accurate. I did track writing and development time during writing, but I already had a lot of material to work with. But here's an overview (numbers in hours):

    image

    That is right: writing a book consumes only a little more than 100 hours! But still, I already had lots of material. I'd say to double the number for an accurate timeframe.

    Now I hear the next question coming... Here's the answer already: Yes, I have a girlfriend. We are working on our home (planning phase is done, searching a contractor at the moment), visiting family, doing daily stuff, blogging, work, ... It al worked out to fit together, but still: there have been some busy moments on evenings and weekends. Thanks, people around me, for being patient and caring during these busy moments!

    Are you getting rich out of this?

    Of course, I can grab a couple of beers (for a couple of times), but don't think writing a book will buy you a car... I just felt that I had lots of valuable information that I had to share, and writing a book seemed like the best option to do that. Creating a "to read"-list? Make sure to add ASP.NET MVC 1.0 Quickly to it.

    kick it on DotNetKicks.com