DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Jul 13

Written by: Joe Brinkman
7/13/2009 2:35 AM  RssIcon

DotNetNuke has a lot of great features that come built-in, but there are often situations where the default implementation is not what the customer wants for their website.  This was a big limitation of early versions of DotNetNuke – changing basic functionality often required the user to customize the core DotNetNuke code. 

In DotNetNuke 2.0 all of that began to change with the introduction of Providers in the framework.  This is also where we began focusing on a rich extensibility model that would allow users to install skins, skin objects, and modules as separate packaged extensions.  Over time, the framework has evolved to include different extension types, and in DotNetNuke 5.0 we moved to a unified extension model that treats all extension types in a similar manner during the install and uninstall process.

One of the extension types that was added in the last couple of years was the Authentication Provider.  Unlike most other providers, an authentication provider is not configured in the web.config and actually looks and behaves more like a module than an actual provider. 

LiveLoginAn authentication provider installs up to 3 UI elements that the framework cares about – a login control, a settings control and optionally a logout control.  It is certainly possible to use more controls if needed, but these are the only three the core framework interacts with directly. Once installed the authentication provider can appear as one option in the standard DNN login.  In the image to the left you can see the standard login on DotNetNuke.com with the LiveID tab selected.  This clearly shows a different UI than the standard username and password.

Recently there has been a lot of discussion around a change made to recent versions of DotNetNuke.  In the past, it was possible to define a custom login page and put the Account Login module on the page.  This allowed you to create an SSL enabled login page or to put a login control directly on the home page.  Unfortunately, users would sometimes change the custom login page setting without putting a login module on the page.  In other cases, the login module would be added to the page, but the user wouldn’t have permission to see the page or the control.  Recent versions of DotNetNuke try to detect this situation and prevent you from setting the custom login page to a location without a login control that is visible to unauthenticated users.

Unfortunately, there are a number of third party login modules currently on the market which are not implemented as Authentication Providers and this new behavior makes it impossible to use them with the latest DNN release.  We’ll work on the behavior change to make it possible to use these modules but I thought that this highlights one of the issues with DNN.  When we introduce an official extensibility point, we then start coding the core to use and rely on extensions which conform to the extensibility model.  If you are coding a skin object that doesn’t conform to the official model then we may well break your skin object in future DNN releases.  The same is true with Authentication Providers.  Had these modules been code as Authentication Providers instead of Modules then they would have continued to function.

The real failure in this case rests with the core team and not the module vendors.  When we introduce new features we often haven’t had the resources to fully document how to take advantage of the new feature.  This is especially true for core API changes.  The end result is that the core team ends up coding to the new APIs long before 3rd party vendors start upgrading their extensions.  The biggest reason for this is because we haven’t done a good enough job in documenting how to use the new feature.

With all of this as background, I thought it would be good to run through exactly how to create your own custom Authentication Provider.  In this example, I am going to start from the standard DotNetNuke Authentication Provider.  For the sake of simplicity, I am going to code this as a WSP project, although the techniques apply just the same to a WAP project (LiveID, OpenID and AD providers are all WAP based implementations).

AuthProviderDemo The first thing I do is make a copy of the DNN Authentication provider which is located in the DesktopModules/AuthenticationServices/DNN folder.  I have renamed my copy to Demo so that we can distinguish it from the original.  I have gone into the Login and Settings controls and changed the namespaces to avoid collision and changed them from DotNetNuke.Modules.Admin.Authentication to DotNetNuke.Modules.Admin.Demo.Authentication.

At this point I have a useable Auth Provider, but I don’t have a method to install it.  I could use the Host/Extensions page to create a new package but since this is my first time I decided to build my manifest file using the manifest from the LiveID Authentication Provider as a template.  After making a few modifications to the manifest to add a CSS file (more on that in a minute) and to fix some file names, I am almost ready to zip everything up.


    
        
            DotNetNuke Demo Authentication Project
            The DotNetNuke Demo Authentication Project is an Authentication provider for DotNetNuke that shows how easy it is to create your own custom auth provider.
            
                DotNetNuke Corporation
                DotNetNuke Corporation
                www.dotnetnuke.com
                support@dotnetnuke.com
            
            
            
            
                05.00.00
            
            
                
                    
                        Demo
                        DesktopModules/AuthenticationServices/Demo/Settings.ascx
                        DesktopModules/AuthenticationServices/Demo/Login.ascx
                        
                    
                
                
                    
                        DesktopModules/AuthenticationServices/Demo
                        
                            App_LocalResources
                            Login.ascx.resx
                        
                        
                            App_LocalResources
                            Settings.ascx.resx
                        
                        
                            Login.ascx
                        
                        
                            Settings.ascx
                        
                        
                            Login.ascx.vb
                        
                        
                            Settings.ascx.vb
                        
                        
                            license.txt
                        
                        
                            ReleaseNotes.txt
                        
                        
                            DemoAuth.css
                        
                    
                
            
        
    

Before packaging the system however, I want to make some changes to the login controls just to make it a little clearer when I am using my new control versus the standard DNN version.  The DNN login uses tables for handling the form layout, and I am going to use a definition list.  I also want to get rid of the help icons next to the form labels and use some different styling on the actual textboxes.  Finally, I want to get rid of the Captcha control.

The first step is to clean up the login HTML


 

After making these changes I make the corresponding changes in my code-behind to remove references to the captcha control and to update the visibility code for the verification control/label.  This is pretty much all that is needed for the Login control.

The next set of changes are made to the settings control.  We need to make sure that we are saving our control settings using unique settings keys (although you are free to store your settings using whatever method you are comfortable with).  In this case I create a new DemoAuthConfig class to replace the core AuthenticationConfig class.  I then go back and update the settings and login pages to reference my new config class rather than the old class.  I also remove all references to the captcha setting since I am not using it here.

Finally, I want my login control to have it’s own look which I may choose to surface on the settings page at some point.  To handle this I include a CSS file in my project since I don’t want end users to have to muck around trying to edit core CSS files.  My CSS file changes the way the definition list is displayed to show up as two columns.

One last bit that I need to update is to edit the login.ascx.resx resource file.  This resource file contains the Title.Text resource which controls the tabname that is used when displaying the various login controls.  In my case I used “Demo Auth” for my Title.Text.

At this point I am ready to package everything up in a zip file so that it is ready for installation.  Installing an authentication provider in DotNetNuke 5.0 is exactly the same as installing a module.  Navigate to the Host/Extensions page and select the “Install Extension Wizard” option from the module action menu.  This will walk you through the installation process. 

Once the provider is complete, you will need to go to the Host/Extensions page and edit the new Authentication Provider settings to make sure it is enabled (it should be enabled by default).  To edit the settings, click the edit (pencil) icon next the the DNN_DemoAuthentication extension listing.

HostSettingsAuthSys

You should see the following settings which allow you to enable or disable this authentication provider for all portals.

HostSettingsAuthSysSettings

Once the provider is enabled at the Host level, you are now free to go to each portal and enable the provider for each portal where you wish to use it.  Just go to the Admin/Extensions page and click on the edit (pencil) icon for the demo auth provider to bring up our custom settings page.  Now we can enable our new AuthenticationProvider.

AdminAuthSysSettings

 

If you have followed along to this point, when you go to the login page, you should see two authentication types – Standard and Demo Auth.  If you look at the Demo Auth page, you will notice that the “help” icons are removed and the text boxes are displayed  using a custom style.

AuthProviderDemoLogin

At this point you should be ready to create your own custom Authentication provider which plugs into the standard login system.  This is the recommended method for providing custom layouts and authentication types for DotNetNuke and I highly recommend you check it out.

Download the demo code DemoAuth.zip

17 comment(s) so far...


Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

How can we do this in DNN 4.x? I followed the steps you specified above in DNN 4.x; however when I tried to install the authentication service, I kept getting an error saying that it's not able to find the DemoAuth_Install on the root of the 'temp' folder where this is being installed. The file does exist in the .zip file though. Or is this just not achievable in dnn 4.x? Please help!

By Linda on   7/24/2009 9:48 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

This was just the push I needed to do a long-neglected project!

By J0e Craig on   7/26/2009 7:28 PM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Did you notice that when you install the demo authentication provider and go to Admin->Extensions->DNN_DemoAuthentication(edit) that any change to the demo (such as checking or unchecking the "Enabled" box) also changes the Default(Standard) "enabled checkbox? How can I persist the demo "enabled" characteristic w/o affecting the default?

By Michael Barnett on   7/28/2009 7:15 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Sorry for my last post. I suppose IIS had cached something because that behavior is no longer happening. I saw where you created the new DemoAuthConfig class but it did not seem to be working. I ran iisreset and now it is working fine. Or maybe I just moused the wrong control somewhere. Anyway I now feel like a dunce. Your Auth provider is working perfectly. Now if I can sync it up with our home-built Single Sign On system all will be well.

By Michael Barnett on   7/28/2009 11:44 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Thank you for sharing this fine piece. Very inspiring! (as always, btw)

By Learn Master Guitar on   8/7/2009 8:27 PM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Can�t believe that I�m the first one to digg this? Anyway, I did. Thanks for this awesome information.

By Harley Davidson Parts on   8/7/2009 8:27 PM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

hi joel, I am getting this error, whenever i want to make changes to the module Error: is currently unavailable. DotNetNuke.Services.Exceptions.ModuleLoadException: Unable to cast object of type 'DotNetNuke.Modules.Admin.Demo.Authentication.DemoAuthConfig' to type 'DotNetNuke.Modules.Admin.Demo.Authentication.DemoAuthConfig'. ---> System.InvalidCastException: Unable to cast object of type 'DotNetNuke.Modules.Admin.Demo.Authentication.DemoAuthConfig' to type 'DotNetNuke.Modules.Admin.Demo.Authentication.DemoAuthConfig'. at DotNetNuke.Modules.Admin.Demo.Authentication.DemoAuthConfig.GetConfig(Int32 portalId) at DotNetNuke.Modules.Admin.Demo.Authentication.Settings.Page_Load(Object sender, EventArgs e) --- End of inner exception stack trace ---

By al on   8/25/2009 6:53 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

I'm attempting to write an authentication provider to authenticate against user details in my own web application. I'm more of a C# person than VB so I have a couple of questions. Presumably the cmdLogin_Click function is the bit I need to modify to check against my own SQL user table. I assume that the UserController object somehow checks the username password and returns true in the loginStatus variable? Assuming I leave the last 4 lines in cmdLogin_Click (the bit that raises the UserAuthenticated event) I can presumably do what I like provided I set eventArgs.Authenticated to true or false? Andrew

By Andrew Jones on   9/3/2009 1:55 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

I believe one of the reason third party login modules not using DNN Authentication Providers is due to that they cannot change the layout of Authentication Providers. (The tabs and 'remember me' controls.)

By George on   9/21/2009 7:15 PM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

very helpful. many thanks. is there any c# version of dotnetnuke available?

By Annuities on   9/28/2009 11:18 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Hi, Will this article can help me if I have to use non dnn database for users, roles and permissions. I would like to do custom authentication using non dnn database in dotnetnuke project. Looking for your valuable response Thanks....

By vindu on   10/6/2009 2:05 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Hi Joe, thank you for great article! I need advice - we are planning to use DNN for our company web site, main requerement (already implemented in our other projects) is to use only Certificate authentication on EVERY PAGE (HttpContext.Current.Request.ClientCertificate) and compare provided certificate data against User table in DNN (extend table) or create new table Certificate in DNN (data already exist for all users in our other systems) Our plan is to create another login module or modify and reuse Brandon module http://blogs.law.harvard.edu/brandonhaynes/2009/05/01/dotnetnuke-multi-factor-authentication-provider/comment-page-1/#comment-2520 Joe, from you experience is durable in DNN ? how complicated ? Thank you Eugene

By Eugene on   10/7/2009 11:10 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Hi Joe, please answer the query I have asked, I have been looking for it for so long.. Even i didnt recieved answer of it in dotnetnuke website.. Thanks in advance...

By vindu on   10/7/2009 10:06 PM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

@Vindu - The custom authentication provider only authenticates the user. If you want to use an alternative method for managing permissions and roles then you will need to write a custom permission and roles provider as well.

By jbrinkman on   10/8/2009 3:02 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Great post. When simply adding this extension to DNN it works fine, but if i make the slightest modification suddenly "DemoAuthConfig" does not exist in "DotNetNuke.Modules.Admin.Test.Authentication" namespace (in the login.ascx.vb Enabled() Property). How can I get around this? Why cant I find the Settings class or DemoAuthConfig from the Login.aspx.vb file?

By Patrik on   10/8/2009 3:53 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Hi Joe, Thanks for the reply, I just want to confirm that if I create custom authentication provider as well as custom permission and roles provider as an alternative method for authenticating user, will it work with dnn authentication along with non dnn database, as I have tried creating custom provider, where my users and roles tables are in non dnn database, in that case it wont work in dnn as dnn overwrites my custom provider. Thanks and waiting for your reply... Thank you so much

By vindu on   10/9/2009 12:27 AM
Gravatar

re: DotNetNuke Tips and Tricks #12: Creating your own Authentication Provider

Also Joe my major concern is to use non dnn database for user authentication, how can i do that in dnn project?

By vindu on   10/9/2009 12:31 AM
Sponsors Minimize
spacer
dummy