Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDNN Open Source...DNN Open Source...Provider and Extension ForumsProvider and Extension ForumsAuthenticationAuthenticationCustom validation advice help neededCustom validation advice help needed
Previous
 
Next
New Post
4/11/2008 3:57 AM
 

I am happy with AspNetMembershipProvider except I want to also validate the user against an external database and pull in some simple data like name, email address and possibly some roles. I think I know how to do this but a couple of development issues are holding me up:

I have created a CustomProviders project and added my new provider class. I was expecting to be able to extend AspNetMembershipProver and override the ValidateUser() method, but:

I can't get the references to work so I can build this custom project.

using DotNetNuke.Security.Membership;
...
public class CustomMembershipProvider : AspNetMembershipProvider

...won't build as it thinks it has an ambiguous reference. My reference dlls only contain Provider.Membership.AspNetProvider

Another problem is how to override the (already overridden in MembershipProvider)  ValidateUser() method in AspNetMembershipProvider? Do I make it overridable in the DNN core source code?

Finally should my custom providers project reside inside the DotnetNuke Solution Items or can it be a totally independent project that is referenced by the website installation?

I have looked around for solutions to the above, but most seem to propose copy and paste of a provider you like and modify it. Perhaps this is the only way if AspNetMembershipProvider hasn't been designed to be sub classed. All advice and pointers gratefully received.

 

 
New Post
4/11/2008 11:33 AM
 

Hi Neil,

You are completely on the right track.  Creating a separate project and referencing your new provider is the way to go (don't forget that you will need to select the new provider via your web.config).  The copy-and-paste approach that some have taken is complete sloppiness.

Your class declaration looks okay.  I would recommend being more explicit about your inheritance, to avoid any sort of wierd naming conflicts, as:

public class CustomMembereshipProvider : DotNetNuke.Security.Membership.AspNetMembershipProvider

{}

Also, looking at my most recent custom provider, I have references to DotNetNuke.dll, and DotNetNuke.Provider.AspNetProvider.dll.  You mention Provider.Membership.AspNetProvider.  Could you be referencing the incorrect assembly?

Finally, I would look in the object browser using the "My Solution" subset to see where the ambiguity might be coming from.  Search AspNetMembershipProvider and see if you get two classes.

Hope this points you in the right direction!

Brandon

Edit: Pre-4th-cup-of-coffee typos.


Brandon Haynes
BrandonHaynes.org
 
New Post
4/11/2008 2:55 PM
 

Thanks Brandon. That was encouraging. I have sorted the referencing out along the lines you suggest and I can now, in my separate project, override AspNetMembershipProvider.ValidateUser(). I have sullied the DNN source by making this method overridable. Do you know of any reason why I shouldn't do this?

Current code, which only needs DotNetNuke.dll and DotNetNuke.Provider.AspNetProvider.dll referenced to compile:

using DotNetNuke.Security.Membership;
namespace CustomLibrary
{
 public class CustomMembershipProvider : AspNetMembershipProvider
  {
   protected override bool ValidateUser(int portalId, string username, string password)
   {
    bool validated = base.ValidateUser(portalId, username, password);
    if (!validated)
     validated = DoCustomValidation(username, password);
    return validated;
   }
   protected virtual boolean DoCustomValidation(string,username,string password)
  { //custom implementation }
  }
}

Lots more to do, but that works as it stands. Now to develop the remaining functionality with TDD and Mocks!

 
New Post
4/11/2008 5:48 PM
 

Hi Neil,

If the ValidateUser function is all you need to override, you might be better off just extending the SqlMembershipProvider (or whatever base ASP.NET membership provider you are using) and overriding the ValidateUser method there.  The DotNetNuke source just executes a call to that function with no additional work (At 540 of AspNetMembershipProvider.vb).  You'll lose the portalId that way, but you don't have to make a core change either.

The last time I extended the DotNetNuke.Security.Membership.AspNetMembershipProvider, I wound up overriding many of the functions therein (UserLogin, ResetPassword, etc).  Just the ValidateUser method would have been insufficient -- I needed to coordinate external webservices on login, password changes, password resets, etc.  There are a lot of subtleties.

Perhaps the most tricky part is dealing with the several scenearios that result between ASP.NET and the external store (success/success, success/fail, fail/success, fail/fail).  Your needs may not be that specific.

Otherwise it looks good.  Just consider my suggestion in the first paragraph to avoid that perilous core change!

Brandon


Brandon Haynes
BrandonHaynes.org
 
New Post
4/14/2008 2:08 AM
 

@Brandon: "avoid that perilous core change!"

That sounds like good advice. I have therefore abandoned the core change and created a wrapper around AspNetMembershipProvider - it looks like I will end up with a Decorator Pattern as I can envisage needing various versions for upcoming clients, and sometimes pipelined authentication (ip, referer, client web service...). I think - as you have found with your implementations - that interaction with the external resource will involve more than just validation.

(Note: I initially avoided a wrapper as I was too lazy to create all the plumbing needed, but it turns out that ReSharper makes it dead easy:-)

I would be very grateful if you have any pointers on how to:

a) Create a new user on the fly if the external resource validates and a DNN account doesn't exist yet

b) Add the user to externally supplied roles.

I have sort of done the roles part, but I need to reload the application to see the effect . Also, I know something isn't quite right as my simplistic approach causes a crashing problem with the RoleController. It is almost certainly my lack of understanding at this level of detail that is the problem. Here is my (test) code :

 

 

Neil

 

internal void UpDateRoles(int portalId, UserInfo user)
{
if (!user.IsInRole("Member"))
 {
   RoleController controller = new RoleController();
   RoleInfo roleInfo = controller.GetRoleByName(portalId,"Member");
   controller.AddUserRole(portalId, user.UserID, roleInfo.RoleID,System.
DateTime.Now);
   controller.UpdateRole(roleInfo);
 }
}

 
Previous
 
Next
HomeHomeDNN Open Source...DNN Open Source...Provider and Extension ForumsProvider and Extension ForumsAuthenticationAuthenticationCustom validation advice help neededCustom validation advice help needed


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out