IIS Authentication plugin for the Wordpress PHP blogging engine

This weekend, I was working on an MSDN magazine article and as part of putting together a demo happened to install PHP’s Wordpress blog app on IIS7.  Of course, I used the latest FastCGI bits available in the box in in Windows Server 2008 Beta 3, and the latest Windows-optimized PHP 5.2.3 build from www.php.net.  Long story short, I ended up writing a Wordpress plugin that allows Wordpress to use IIS authentication methods, including forms authentication.

I was pretty happy to observe that Wordpress installed and worked seamlessly on Windows Server 2008.  To install it, I ended up doing the following:

1.       Check Wordpress requirements.

2.       Install MySql Community Server 5.1 and associated GUI tools.  I disabled TCP connectivity, and enabled named pipes only since I was using it locally.

NOTE: When trying to connect to the MySql instance using the MySql Administrator, be sure to use “.” as server name, not “localhost”.  Use “root” as username with the password you set up.  Click Advanced and enable named pipes to connect, and specify the instance name as the pipe name (“MySql” for me).

3.       Download latest PHP 5.2.3, Windows Non-Thread-Safe build.

4.       Follow the Wordpress “Famous 5 minute install” steps, creating the MySql DB using the MySql command line tool. Be sure to write down the "admin" user's password that wordpress generates.

5.       Create IIS site, rooted at the wordpress directory root.  Put it on localhost, with “myphpblog” host-header binding.  Added the host-header to %windir%\system32\drivers\etc\hosts file so I can test with http://myphpblog/ locally.

6.       Added “index.php” default document to the site

7.       Set up the PHP via FastCGI handler mapping as I describe in the PHP with FastCGI article.

I have to admit that I’ve never written any PHP code worth mentioning before, since most of my development is split pretty evenly between C# (for .NET) and C++.  However, PHP seems to be a pretty simple language to get started with, especially if you are not in need of using any extensions that would require some learning about.

Wordpress itself provides a pretty neat extensibility model, allowing you to drop in plugins that either replace the definition of some Wordpress functions (such the most of the ones used for authenticating users), and intercept other events via filter and action hooks.  Most of these are not extremely well documented, so it took a bit of time of groveling through Wordpress code to figure them out.   Thankfully, findstr makes pretty quick work on finding function references in plain text .PHP files, so a few NY minutes later the hook stuff becomes clear enough to get started.

In the end, I didn’t end up using Wordpress for the article (nothing bad against Wordpress, but I found another app more suitable for my demo), but not before I ended up writing the Wordpress IIS Authentication plugin.

IIS Auth plugin for Wordpress


In a nutshell, the plugin allows Wordpress to recognize IIS authentication methods, allowing the user to log in with an IIS authentication method such as Windows Authentication, Basic Authentication, or the ASP.NET forms authentication.

The Wordpress blog engine would then recognize the IIS user and use it for things like writing posts, leaving comments, or performing blog administration. 

This is nice because it allows you to integrate the app with the rest of your website which may be using a particular IIS or ASP.NET authentication mechanism, so that users log into wordpress the same exact way they log into the rest of your application.

NOTE that the user account still has to created in with Wordpress, so that it can do internal access control when the username is provided to it by the IIS Auth plugin.  A complete solution would basically completely remove the need to maintain the user accounts with Wordpress, and use whatever credential store the IIS authentication mechanism was using instead, but doing this (if it is at all possible) was out of scope for me.  Perhaps, one day someone could build a WP plugin that replaces its credential store with one that uses ASP.NET’s Membership service, and thus works with different credential stores (hint hint).

To illustrate this, here is what happens when I click the WP login link after setting up the IIS Auth plugin, and configuring ASP.NET forms authentication for the blog website:

Use Forms Authentication for Wordpress with IISAuth

As you can see, I can login in with my makeshift ASP.NET login page with the unskinned Login control.  I can now log into Wordpress by logging into my website with my ASP.NET Membership credentials.

If I configure basic authentication instead, I get:


Using IIS Basic Authentication for Wordpress with IISAuth

As you can see, I can log in with basic authentication instead of the Wordpress login form.

Step by step installation instructions

1.      Set up an IIS authentication method you want to use to access Wordpress. 

If you are using IISAuth, you probably already have this set up.  I tested with both Windows Authentication, Basic Authentication, and Forms Authentication.  You can configure this using the IIS7 Admin tool – select the website/application node where WP is installed, and click the Authentication feature icon.  Here is me configuring Forms Authentication:

Configure IIS authentication method with IIS7 Admin tool

Admittedly the process is easier for using Windows Auth or Basic Auth, if you don’t already have Forms authentication / Membership set up.  I used forms auth with a sample XML Membership provider – you can find it in the attached sample application.

Also note that I am leaving Anonymous Authentication enabled for now, because I am going to need it to be able to access Wordpress until I the IIS Auth plugin is activated.

2.      Create the “admin” user so you can access the WP admin console and create other users as necessary without locking yourself out J

If using basic or windows authentication, create the admin windows user, and make sure to make it a Member of IIS_IUSRS.  DO NOT give this user Administrator privileges on the machine, or make it a member of the Administrators or other powerful groups.  You should also disable the right of this user to do interactive logons to the machine.  This is just a user with which we will log into Wordpress to create other users / perform administrative tasks for the blog once we turn on IIS Auth.

I used Forms authentication / membership so I went and created a Membership user.  You can create one from the IIS7 Admin tool by clicking the .NET Users feature (if you are using the built-in SqlMembershipProvider).  The Admin tool won’t let you create a user for a non-GACed Membership provider, so you would have to create it yourself using code or your website’s user registration page.

3.     Drop the IISauth.php plugin into the wp-content\plugins\iisauth directory (create it since it won’t exist)

4.     Go the WP administration console , and log in with the WP’s “admin” user (the password was created and given to you when you installed  wordpress.  Don’t lose it! If you don’t have it, reinstall).

5.     Click the plugins link on the top, and on the plugins screen, activate the IIS Auth plugin

Activate IIS Auth plugin for Wordpress

You should see a message telling you that the plugin has been activated.  At this point, the IIS Auth plugin is in effect, and whenever you click the login link, you will be prompted to log in using whatever IIS authentication method you configured.

6.     OPTIONAL: Disable Anonymous Authentication if needed

If you are not using Forms Authentication, at this point you can go back to the IIS7 Admin tool, like we did in step 1, and disable anonymous authentication.  Do this only if you want all access to Wordpress to require the user to log in. 

If you are OK with the user browsing WP anonymously, until login is required, leave anonymous authentication on (most cases).  The user will be challenged to log in as soon as Wordpress determines that they should be logged in, such as when you try to access the admin part of the blog, or when you click the Login link.

7.     Create other Wordpress users for each IIS user that you want to be able to log into Wordpress

Use the “admin” user to log into WP’s admin console, and then you can create a user for each of the IIS users you want to have Wordpress accounts.

Download IIS Auth plugin for Wordpress


Download IIS Auth for Wordpress, version 1.0.  This download contains a sample application, with a sample XML membership provider, forms auth login page, and the IIS Auth plugin located in the wp-content\plugins\iisauth\iisauth.php.  You can copy this into your wordpress root to get started, and examine the web.config.sample for the desired settings – or just grab the iisauth plugin from there.

As usual, this is released under the Microsoft Permissive License, which means that this is distributed as is, with no warranties, and you can do whatever you want with the code, as long as you retain this license and all attributions present in the software.

Quick limitations of version 1.0:

1.       You can use this on IIS 5, IIS6, and IIS7, although using it with Forms Authentication requires an application running using IIS7 Integrated pipeline.  You can still use it on IIS5/6 for Windows Authentication or Basic Authentication.

2.       Logout for Forms Authentication requires a login.aspx page in the root of the site, that supports the ?action=logout querystring parameter and performs Forms Authentication logout.

3.       Logout for all other authentication methods is performed by asking you to close the browser window.  This is how logout must be performed for basic authentication, or windows authentication, since the browser caches the logon credentials and not all browsers correctly clear the cache.

That’s it – happy blogging.  Leave any bug reports, feature requests, and other feedback here.

Thanks,

Mike

Published 12 August 07 11:28 by Mike Volodarsky
Filed under: , ,

Attachment(s): iisauth_wordpress_v1.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Mike Volodarsky's WebLog said on August 13, 2007 12:10 AM:

This weekend, I was working on an MSDN magazine article and happened to install PHP’s Wordpress blog

# iis said on August 13, 2007 12:36 AM:

This weekend, I was working on an MSDN magazine article and happened to install the Wordpress PHP blog

# Noticias externas said on August 13, 2007 12:54 AM:

This weekend, I was working on an MSDN magazine article and happened to install the Wordpress PHP blog

# Woil said on August 13, 2007 8:33 AM:
Thanks! A while back I tried to figure out how to do this for my own PHP app, and will now be able to do it with your code. Much appreciated.
# Mike Volodarsky said on August 13, 2007 10:02 AM:

Glad to hear - let me know how it works out.

Mike

# iis said on August 14, 2007 12:27 PM:

Hello, my name is Drew Robbins. I just started as the IIS Technical Evangelist focused on developers

# Matt said on August 31, 2007 8:24 AM:
Any thoughts on when FastCGI will be supported on IIS6?
# Mike Volodarsky said on September 4, 2007 8:24 AM:

Matt,

FastCGI is available on IIS6 as the FastCgi Technical Preview 2, and we are working on a beta release that should be available soon.

You can find out more here: http://mvolo.com/blogs/serverside/archive/2007/05/29/The-latest-on-the-FastCGI-project-and-PHP-support-on-IIS.aspx.

Thanks,

Mike

# Mike Volodarsky's ServerSide said on September 24, 2007 5:12 PM:

At last, the IIS FastCGI Go-Live release is here! This release enables you to begin hosting PHP applications

# Luca said on October 24, 2007 6:03 AM:
Hi, I'm Luca I'm using your plugin with wordpress 2.2.1 and I have some problems with some admin functions. Adding a category or adding an user I get a message "You don't have permission to do that". I can modify an existing user or a category. If I disable the plugin all works fine, I can add new users and new categories, even without relog to wp. Any idea of how I can fix that? Thanks, Luca
# Mike Volodarsky said on October 24, 2007 12:06 PM:

Hi Luca,

What IIS authentication mechanism are you using with the plugin?  Be sure that you have it enabled for all urls of the website.

Thanks,

Mike

# Mike Volodarsky's ServerSide said on October 28, 2007 3:02 AM:

Over the past several year, I've written quite a few modules for IIS7 / ASP.NET. Some of these were

# Chris said on November 14, 2007 3:43 PM:
I've strongly typed and compiled the included .cs file but it won't install into the GAC. It's complaining about the assembly name not matching the file name. I've triple checked and no dice. Am I even barking up the right tree? Any help for a newbie?
# Eric Schwarz said on November 27, 2007 12:25 AM:
I have setup up windows web server 2008, have mysql and php some what running wordpress. Wordpress work great until i connect to a php generated url, then all i get is a blank page, funny part is the reqest goes through and can see the change example of white screen of death (http://192.168.1.33/wp-admin/themes.php?action=activate&template=classic&stylesheet=classic&_wpnonce=25e54c5875). even more of a pain to a notivce like me is that (http://192.168.1.33/?cat=1) works. help me out my email is Schwarttzy at Microsoft server network dot com thanks! Also any tips to getting FastCGI working i can only get CGI, i get error 500 with fastCGI.
# Christophe Lauer, Blog Edition said on December 5, 2007 9:23 AM:

Voici une vidéo présentée par un de mes collègues de Microsoft qui détaille les étapes nécessaires pour

# Noticias externas said on December 5, 2007 10:11 AM:

Voici une vidéo présentée par un de mes collègues de Microsoft qui détaille les étapes nécessaires pour

# Mike Volodarsky said on December 8, 2007 12:55 PM:
Hi Chris, The supplied membership provider is just a sample, I wouldnt recommend using it in production. Its based on http://msdn2.microsoft.com/en-us/library/aa479031.aspx. For a more secure XML based membership provider sample, check http://www.codeplex.com/aspnetxmlproviders (this is not a Microsoft product, so no warranties). That aside, you need to sign the compiled assembly with a private key in order to be able to install it in the GAC, and then you will need to specify the full strong name in order to reference the type in the providers section. This is pretty easy if you are using Visual Studio. Otherwise, see http://msdn2.microsoft.com/en-us/library/xc31ft41(VS.71).aspx. Thanks, Mike
# ServerSide with IIS 7.0 said on December 20, 2007 3:37 PM:

This month, the Enhance Your Apps With the Integrated ASP.NET Pipeline article is finally out in MSDN

# oyun said on December 30, 2007 6:54 AM:
Glad to hear - let me know how it works out.
# Tim McCormack said on March 25, 2008 2:36 PM:

You know what would be a sweet modification to this plugin? Having the plugin automagically create the WordPress account upon login.

There's another delegated-authentication plugin that already does this (<a href="http://wordpress.org/extend/plugins/openid/">OpenID for WordPress</a>) -- maybe you could grab the relevant code from that.

# SergioTarrillo's RichWeblog said on April 27, 2008 8:53 PM:

Para empezar a probar IIS 7 sobre Windows Server 2008, podemos descargar Windows Web Server 2008 , lo

# ASP.NET Espanol Blogs said on May 20, 2008 3:16 PM:

Para empezar a probar IIS 7 sobre Windows Server 2008, podemos descargar Windows Web Server 2008 , lo

# creationpool said on May 29, 2008 10:20 AM:
Great Thanx.
# vic said on June 24, 2008 3:03 PM:
Hi there, Know anyone which plugin to use if I want only the first part (first paragraph) of the article to be displayed? If someone want to read more there will be a "read more" link... On my blog right now is displayed all of article content and I don't want this... I'm talking about the latest 10 articles displayed on the first page of my blog. I'm using Wordpress. Can anyone help?
# anon said on August 11, 2008 10:53 AM:
hi, i am using IIS7 to run PHP - the issue is that when i use certain authentications there seems to be some file permission errors. For example when i use Windows authentication i am not able to write into a file. For this i have no clue as to which user group i need to give the file permission. On the other hand while using the basic authentication i do not get this message. Initially i had the issue with the anonymous authentication as well bit i rectified this by given write access to all users of my machine (though i know this is not the right way). What am i missing for Windows authentication. The same code used to work without any issues in IIS6 with the same set of permissions. This is the error i get : Warning: fopen(file.txt) [function.fopen]: failed to open stream: Permission denied in ]. Any help would be highly appreciated. TIA
# Tom said on August 20, 2008 4:39 AM:
to Anon: 11/Aug .. Im having hte same problem, dont suppose you found a solution yet?
# Mike Volodarsky said on August 21, 2008 5:04 PM:

Hi Anon, Tom,

If you'd like your PHP script to run with the identity of the authenticated user, you'll need to set fcgi.impersonate=1 in PHP.INI. This is needed to impersonate the windows auth/basic auth users, or the anonymous user.

If you dont do this, the PHP script will execute with the identity of the IIS application pool.

If you dont want user impersonation, and keep this setting off, then simply granting IIS_IUSRS access to the content should be sufficient.

Thanks,

Mike

# Jesus said on August 27, 2008 11:41 AM:
Hello. Does anybody tried it on Wordpress 2.6?? I´m trying to integrate it on an IIS web site that authenticate previously users using digital certificates, this is working, and I pass IIS authentication with digital certs, and now I wanted to use the plugin for single sign on on wordpress. I have created the same users on windows, certs and wordpress, but when I activate the plugin, I get a "You don´t have permission to access" page inmediately. Thanks!! Jesús

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below


About Mike Volodarsky

For the past 5 years, I was the core Program Manager for Microsoft ASP.NET 2.0 and IIS 7.0 products. I drove the design and development of the IIS 7.0 web server core, the IIS FastCGI support, the AppCmd command line tool, the ASP.NET Integrated pipeline, and other special projects around server security, performance, and scalability. Now, I am working on my own on cutting edge web server tech on top of the Microsoft IIS platform, and continue blogging about it here.

About me



For the past 5 years, I was the core server Program Manager for the IIS 7.0 and ASP.NET 2.0 products at Microsoft.
Now, I work on advanced web server tech using IIS 7.0, .NET, and Windows Server 2008 and write about it in this blog.

View Michael Volodarsky's profile on LinkedIn

Writings



TechNet Magazine
>Top 10 Performance Improvements in IIS 7.0

MSDN Magazine
>IIS 7.0: Build Web Server Solutions with End-To-End Extensibility
>IIS 7.0: Enhance Your Apps with the Integrated ASP.NET Pipeline
>IIS 7.0: Explore The Web Server For Windows Vista And Beyond
>Design and Deploy Secure Web Apps with ASP.NET 2.0 and IIS 6.0
>Fast, Scalable, and Secure Session State Management for Your Web Applications


Tools and Modules

LeechGuard
IconHandler 2.0
DirectoryListing
HttpRedirection
IIS Auth for Wordpress
iisschema.exe
PortCheck.exe v2.0

Popular Posts

- ASP.NET 2.0 Breaking Changes on IIS 7.0
- Develop IIS7 modules and handlers with .NET
- Troubleshoot IIS7 errors like a pro
- Troubleshooting 503 / "service unavailable" errors
- Troubleshooting "server not found" errors
- Create IIS7 sites, applications, and virtual directories
- Run Ruby on Rails with IIS FastCGI
- VS Debugging of ASP.NET applications on Windows Vista
- Stop hot-linking with IIS and ASP.NET

Tags

Search

Go

This Blog

Archives

Good IIS Blogs

Disclaimer

These postings are provided as is with no warranties, and confer no rights. The views expressed in this blog are entirely my own.

Syndication