How to write custom DNN login module?

Download Custom DNN Login Module (43.27 kb)

These days I am working on putting together a DNN site. One of the requirements for the site is to have a login page that is different from what DNN offers out of the box. There are some third party DNN modules available that allow you to customize the look of the login page all together. But my requirements are something that does not fit into any of the modules available out there. So I was on mission to develop my own DNN login module.

If you understand how default login process works and what module gets displayed when you click on login link on the page, then things are very stright forward.

Click on Login link

If you look at the skin that you are using, you will find that it is using Login control that is present in admin>skins folder of your DNN web site folder. Key to the whole process is cmdLogin_Click event handler method that gets called when you click on Login link or button on your page. If you look at the code, you will find the following set of code that is trying to find URL to where request should be redirected.

Response.Redirect(LoginURL(ReturnUrl, (Request.QueryString("override") IsNot Nothing)), True)

This LoginURL method is the one that decides what page will be shown for login. This method is implemented in core DNN library. This is present in DotnetNuke.Common namespace in Globals.vb file. I will not post the code here but I will explain it how this discovery of login page works.

First it is looking for a tab id for login page. This is critical piece that you will need to know. If login as Admin on the site and goto Site Settings menu item, you will find the following set of controls on there under Advanced Settings that allows you to configure some of the key pages of your site. The drop down for Login Page is where you will configure what page to show when user clicks on Login link. If this entry is set to Not specified that means tab id fotr login page in Portal Settings is -1. LoginURL looks at the TabID for login page. If this value is not set meaning the value is -1, then code redirects the users to default DNN login page.

But if you have a page set in Login Page dropdown then LoginURL finds all the modules on that page. Then it iterates over all the modules and check if there is atleeast one module that has frieldly name of Active Login. Following code is from Globals.vb that shows this implementation.

 Public Function ValidateLoginTabID(ByVal tabId As Integer) As Boolean
  Dim hasAccountModule As Boolean = Null.NullBoolean
  For Each objModule As ModuleInfo In New ModuleController().GetTabModules(tabId).Values
  If objModule.ModuleDefinition.FriendlyName = "Account Login" Then
    'We need to ensure that Anonymous Users or All Users have View permissions to the login page
    Dim tab As TabInfo = New TabController().GetTab(tabId, objModule.PortalID, False)
    If TabPermissionController.CanViewPage(tab) Then
      hasAccountModule = True
      Exit For
    End If
  End If
  Next

  Return hasAccountModule
 End Function

Now this is a big problem. This is friendly name of login module that DNN supplies with the framework. So in a way you are forced into using the built in login module. So if you do not have this module on the page, then you will get following error.

You have been redirected to this default Login Page because the Login Page set for this website does not contain a valid DotNetNuke Account Login module, or the permissions have not been set correctly.

Does this mean that you can not build a custom login module. Answer is simple. Yes, you can build your own custom module but you will just need to work around this bug or feature or whatever you want to call it. I will discuss that shortly.

Build Custom DNN Login Module

Now you can go ahead build your custom module that you want to use for login. I will leave those details to you. I have included a sample custom login module that I have put together by copying implementation from DNN login module and tailored it to my needs. Once you have developed the module, add a page into your portal. You can choose the option of not showing in menu. Now goto Admin > Site Settings > Advanced Settings option. Under Page Management section, in Login Page dropdown list, select the page that we just added for login. On this page, add your custom module. Now add Active Login module that ships with DNN framework. Now in settings of this Active Login module, set the option that it is visible to Administrators only. This will ensure that all conditions of login module are satisfied on the page to include your custom login module. And this default login module will stay hidden from your users. And you are all set to go!

Caution

Login module is very crucial module in your portal. If you mess this up, you can make your site unusable because nobody will be able to login including you as super user. Now you can see why DNN has some kind of check on module on the page to decide if they want to allow custom login module or not. Here are some of my suggestions that I follow when doing custom login module development.

  • When starting development and initial testing of the module, do not make changes in site settings to direct login link to your new login page. Initially have your login page visible in menu or access it directly test out your module. Once you are satisified with it, then make site settings change.
  • If you do mess up the module and find your self in situation where you can't login. You have few options.
    • Go into the database and remove the portal settings for Login tab id.
    • If you can't find the entryies in database, then in ValidateLoginTabID method that i mentioned above, force it to return False all the time. This will make sure that you will get redirected to built-in login page.

Sample Custom DNN Login Module

The attached sample is custom DNN login module developed using DNN5.5 libraries in Visual Studio 2010. I have just added bare minimum code to show you how login works. You can extend this module to customize to your need and taste. If you have any quesitons, feel free to contact.

comments powered by Disqus

Blog Tags