Using Orchard's MembershipService from another Project?

Topics: Customizing Orchard, Writing modules
Aug 26, 2011 at 4:41 PM
Edited Aug 26, 2011 at 5:00 PM

Hey everyone,

Originally we were trying to load a complex MVC website into Orchard as its own module, but this opened up a large can of worms, so we abandoned it and are now running the Orchard CMS app as a child application to the parent site in IIS. It's not recommended, but it's working.

However, Orchard CMS uses it's own custom MembershipService, while our parent app uses the built in SqlMembershipProvider, so it's impossible for them to use the same Membership data. 

What I can do is access the User object from within the Orchard.Users project, which will get the current logged on User in the parent site (using the native SqlMembershipProvider). However, this only gives me the Username. If I access Membership.GetUser(username) from within Orchard.Users, it throws a 'The operation is not valid for the state of the transaction'. 

I'm wondering if there is any out-of-this-world way to access Orchard's custom MembershipService from within an entirely separate project (that project being the parent project in IIS).

Does anyone have any experience doing something like this, or have any suggestions?

Thank you,

Ryan

 

UPDATE:
I understand this might be impossible due to Orchard loading everything via dependency injection with Autofac, but am hoping someone can definitely answer that. Thanks.

Coordinator
Aug 26, 2011 at 6:52 PM

Membership providers are not particularly hard to write, and there are actually samples on MSDN. You could write one that has all the hooks that you need, and use this on both sides.

Aug 26, 2011 at 6:58 PM
bertrandleroy wrote:

Membership providers are not particularly hard to write, and there are actually samples on MSDN. You could write one that has all the hooks that you need, and use this on both sides.

I started to write my own MembershipProvider and have that working. Though I think that all of the Orchard modules expect an Orchard IUser, so even though I can access my parent project's Membership data, I think I am still forced to create an Orchard User and return that, in a sort of adaptive way.

Coordinator
Aug 26, 2011 at 7:01 PM

Oh, well, of course you also need to implement the Orchard side that will use the new provider. All the extensibility hooks are there.

Nov 4, 2011 at 6:01 PM

I'd be interested i any info/pointers on a roll your own membership provider that hooks into both orchard and a standard asp.net site. Thanks!

Nov 4, 2011 at 6:09 PM

@jacinthegreen

You can replace Orchard's authentication service. Take a look at the AlexZh.WindowsAuthentication module, which does this for Windows Authentication. It takes just a few small tweaks to allow it to work with Windows Forms. 

 

Then you can replace the MembershipProvider (the default being Orchard.Users/Services/MembershipService.cs). Just override the methods, and add these attributes:
    [UsedImplicitly]   
    [OrchardSuppressDependency("Orchard.Users.Services.MembershipService")]   

   public class GeneralSnusMembershipService : IMembershipService {

    ...definitions...

    }

 

To get this to be able to work in a normal .net app (which I haven't tried), you have to add a reference to Orchard.Core.dll and Orchard.Framework.dll, but I have some doubts that it'd work as Orchard needs to do all of its initialization in its own app, where it "stays". So I think you might need to write a WCF service provider which polls the orchard site from your normal site's custom membership prodiver.

Hope that provides some help.