Posts tagged ‘membershipprovider’

How to write a custom ASP.NET Membership Provider ?

I’ve googled last week information on how to write a custom ASP.NET Membership Provider. I needed it because I wanted to integrate my next Intranet with Alfresco.

Since I use .NET, I just had to connect my ASP.NET code to Alfresco Webservices.

This is the story how I managed to do that ! (Nothing wonderful, it’s really simple though ….)

The first thing I did was to insert Alfresco Webservices as Service Reference in my Web Site Solution under Visual Studio. To do that, just go and look at Alfresco’s wiki that is available here : http://wiki.alfresco.com/wiki/Web_Service_Samples_for_.Net

Once done, just add a new class in your WebSite heritating from System.Web.Security.MembershipProvider

Ask R# to generate the implementation stub and you’re nearly done !

The only method we need to make our first test is the ValidateUser(string username, string password)

Inside this method do something like this :

public override bool ValidateUser(string username, string password)
{
 //Use alfresco Service
 var authenticationService =
    new AuthenticationService.AuthenticationServiceSoapPortClient("AuthenticationService");
 bool onError = false;
 AuthenticationResult res=null;
 try
 {
  res = authenticationService.startSession(username, password);
  authenticationService.Close();
 }
 catch(Exception)
 {
  authenticationService.Abort();
  onError = true;
 }
 if (onError)
  return false;
 if(res!=null)
 HttpContext.Current.Session["AuthRes"] = res;
 return true;
}

Then we need to activate this MembershipProvider in our web.config file :

<membership defaultProvider="AlfrescoMembershipProvider">
 <providers>
  <clear/>
  <add name="AlfrescoMembershipProvider" type="YourNamespace.Utils.AlfrescoMembershipProvider"/>
 </providers>

</membership>

You’re done ! You can test and see that when you log in using a Login ASP.NET 4.0 component, it’s easy to customize which datasource you want to connect to !