.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

WordPress w/ Forms Authentication on IIS6

May 21, 2008 By: Dave

I know I said yesterday that I’d start a series about creating DotNetNuke modules, but I solved a problem yesterday after I wrote that post that I think a lot of you will be interested in.  Especially if you are using WordPress in combination with an ASP.NET site.

The problem we had was this.  We have an ASP.NET web site that requires a login before anyone can see any pages.  We wanted to add a WordPress blog to it that could only be viewed when people log in and wanted to be able to have the same user names in WordPress that they had in ASP.NET.

I did see one plug in that would let us log in to WordPress using forms authentication.  But, it only works under IIS7.  We are still using IIS6, as most of the world is, so that solution wasn’t going to work.

So, here’s what we did.  Most of the work was on the WordPress side, which required a bit of PHP knowledge.  I’ll be the first to admit that I know very little PHP.  But, I do know enough to hack it when I have to.  So, on the ASP.NET side, all I did was set a cookie to the username after the user logged in.  That gets the username some place where WordPress can see it without too much effort.

To force WordPress to require a login, we used the Authenticate plugin.  So, the only real work we needed to do was to create a system that forces the user to use the Login.aspx page on the main site, create a new user if they user doesn’t exist, and log the user into WordPress.  Since only one or two users need special privileges, we left assigning roles to the user as a manual process.

Here is our PHP code, commented so you can see how we got this working.  This code should replace wp-login.php.  There is probably some elegant way of making this work as a plug-in, but I’m not really a php programmer, I just play one on TV.  If you know how to make it into a plug-in, let me know.

<?php
require( dirname(__FILE__) . '/wp-config.php' );
require_once( ABSPATH . WPINC . '/registration.php');

function smartLogin() {

echo "start smartLogin";

    $errors = new WP_Error();

  // If no cookie was set, they have not
  // logged into the main site.
    if(isset($_COOKIE['wpuser_zzz']))
    {
        $user_login = $_COOKIE['wpuser_zzz'];
    }
    else
    {
    // If they aren't logged in, see what
    // WordPress page they were trying to access
        if( isset($_REQUEST['redirect_to']) )
            $returnUrl = '?returnUrl=' .
          urlencode($_REQUEST['redirect_to']);
        else
            $returnUrl = '?returnUrl=' .
          urlencode(get_option('siteurl'));

    // and send them to the login.aspx page with
    // the page they were trying to get to as the
    // returnUrl parameter.
        header('Location: ' . get_option('siteurl') .
        '/../login.aspx' . $returnUrl);
        exit();

    }

    $user_login = sanitize_user( $user_login );

  // If the user doesn't exist in WordPress yet, create them
  // use the md5 hash of the username as the password
  // (so they can't guess it... you may want to salt the md5)
    if ( !username_exists( $user_login ) )
        $user_id = wp_create_user( $user_login, md5($user_login), "" );

  // Once the user is created, log them in.
    wp_login($user_login,md5($user_login));
    wp_setcookie($user_login,md5($user_login),true);
    wp_set_current_user($user_id,$user_login);

  // Now, redirect them back to the page
  // they were trying to get to
  // or the main blog page if you can't find
  // the original page
    $redirect_to = get_option('siteurl');
    if ( isset( $_REQUEST['redirect_to'] ) )
        $redirect_to = $_REQUEST['redirect_to'];

        header('Location: ' . $redirect_to);
        exit();

    return $user_id;
}

// call the function above
smartLogin();

?>

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • CMS vs Code It Yourself
  • Just an Observation…
  • jQuery – Date Picker
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: asp.net, forms authentication, iis6, php, WordPress

12 Responses to “ WordPress w/ Forms Authentication on IIS6 ”

  1. # 1 Lloyd Budd Says:
    May 21st, 2008 at 9:43 am

    Very cool addition to the WordPress toolbox.

  2. # 2 Kaly Says:
    October 17th, 2008 at 3:58 am

    Hello There,
    I haven’t tried the code yet, but before doing so, what about the admin account in wordpress? the one you enter when installing wordpress?

  3. # 3 Dave Says:
    October 17th, 2008 at 5:53 am

    The easiest thing to do is to create a user using this method. Turn off this method long enough to login as admin and set the new user to have admin rights and then turn this back on. Admin won’t be available while this is turned on.

  4. # 4 Kaly Says:
    October 17th, 2008 at 6:41 am

    Thanks for your reply Dave,
    but the script doesn’t seem to work with me.
    When I replaced the wp-login.php file with your script, the first problem i faced was :
    Warning: Cannot modify header information – headers already sent by (output started at C:\Program Files\xampp\htdocs\wordpress\wp-login.php:16) in C:\Program Files\xampp\htdocs\wordpress\wp-login.php on line 40

    Then I took off the “echo ’smartLogin’; line and it worked…
    Now the next problem is that I don’t think the script is creating any usier, in other words, if i click on LOGIN in wordpress, it redirects me to the login.aspx page but that’s the only thing that it’s doing, it’s not creating the cookie and it’s not creating any user in wordpress DB

    Any ideas?

    PS: will this script work if I’m using two seperate domains? if wordpress is hosted on a unix server and my .net application is hosted on a windows server?

    Thanks
    Kaly

  5. # 5 Dave Says:
    October 17th, 2008 at 6:46 am

    Cookies are domain specific. So, no it won’t work.

    We placed our blog as a subdirectory under our asp.net site.

    You MAY be able to get it to work across domains by doing something with redirects and passing parameters but that sounds like a recipe for a security issue to me.

  6. # 6 Kaly Says:
    November 6th, 2008 at 8:43 am

    Hello Dave,
    Is there a demo Login.aspx page for this method? I don’t seem to get it to work properly coz I lag knowledge in .NET
    Thanks

    Kaly

  7. # 7 Dave Says:
    November 6th, 2008 at 8:52 am

    Sorry, I’m not sure that the code would help you if you don’t know .NET

    If you are using .NET 2.0 or greater, you’ll need to trap the authentication event handler to set the cookie once the use has been authenticated. If you are using .NET 1.1 you’d have to roll your own authentication anyhow.

  8. # 8 Kaly Says:
    November 10th, 2008 at 3:58 am

    Hello Dave,
    I finally got it to work !
    The only problem I’m facing is the logout !
    When I click logout nothing’s happening, maybe a code should be added to delete the cookie on logout?

  9. # 9 Elie Says:
    November 10th, 2008 at 7:45 am

    Kaly is right
    the logout isn’t working

  10. # 10 Dave Says:
    November 10th, 2008 at 7:51 am

    Right, you’ll need to delete the cookie.

  11. # 11 Kaly Says:
    November 11th, 2008 at 4:09 am

    Hello Dave,
    I know you have to delete the cookie, I created a button in the sidebar that links to a php page with a simple code to delete the cookie we created earlier, but it’s not deleting anything, is there an easier way to implement it with the logout button of wordpress?

    Thanks

  12. # 12 Khalil Says:
    December 3rd, 2008 at 12:56 pm

    Hi Dave,
    the function is working properly, but the problem is that when the user is logs out, and after deleting the cookie we created in the asp page, the user is considered still logged in, because wordpress cookies aren’t deleted, any solution for that?

← Creating DotNetNuke Modules
Creating DNN Modules – The Tools →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    May 2008
    S M T W T F S
    « Apr   Jun »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.