1

I've used Membership Providers in web apps over the last several years. I now have a new "request" for an internal project at work. They would like a service (not a web service) to do a quick authenticate against. Basically, exposing the ValidateUser(UserName, Password) method...

I am building this in a DLL that will sit with our internal web site. What is the best approach to make this work? The DLL will not reference the web app and the web app will reference the DLL. How do I make the DLL aware of the Membership Provider?

TIA

PS: If this has been answered elsewhere please direct me to that...

EDIT: I found an article on using ASP.NET Membership with WinForms and/or WPF applications. Unfortunately, these depend on an app.config file. A DLL appears to not use the app.config once published. If I am wrong, please set me straight! The article is here: http://aspalliance.com/1595_Client_Application_Services__Part_1.all

Keith Barrows
  • 24,802
  • 26
  • 88
  • 134
  • Are you talking about the SqlMembershipProvider? I'm kind of confused about what you're trying to accomplish. The SqlMembershipProvider is essentially already in a dll (System.Web.dll actually) and it usable just by configuring the machine.config/web.config. – Greg Dec 29 '10 at 19:24
  • Yes, the SqlMembershipProvider. However, I am running it from my own DLL and not a web app per se. The DLL does not have access to a web config. So the question is how to allow SqlMembershipProvider the ability to connect when it does not have configuration info. – Keith Barrows Dec 30 '10 at 16:42

2 Answers2

2

Well, it appears that the only way to set the connection string of a SqlMembershipProvider is to use the Initialize method, which according to the documentation, shouldn't be called from our code.

Initializes the SQL Server membership provider with the property values specified in the ASP.NET application's configuration file. This method is not intended to be used directly from your code.

So basically we have to figure out a way to get that information into a config file associated with the application that's hosting the dll.

All you have to do is reference system.web.dll and system.configuration.dll in your assembly, use the SqlMembershipProvider, and then set the proper values in the app.config for executable like so:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MembershipConnectionString" connectionString="connectionstringdetails" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <membership defaultProvider="DefaultSqlMembershipProvider">
      <providers>
        <clear />
        <add name="DefaultSqlMembershipProvider" connectionStringName="MembershipConnectionString" type="System.Web.Security.SqlMembershipProvider" />
      </providers>
    </membership>
  </system.web>
</configuration>

The important thing to note is that if you "MyAssembly.dll" and "TheApp.exe", this should be "TheApp.exe.config", not "MyAssembly.dll.config". The config file is always associated with the executing assembly.

Greg
  • 16,540
  • 9
  • 51
  • 97
  • There is no (.NET) executable. In one instance it will be called by a ColdFusion app. – Keith Barrows Jan 10 '11 at 22:04
  • I have no idea how that works, I didn't even know you could do that. Without understandhing how cold fusion works or how it can host .Net without an support for config files, I'd suggest trying the Initialize method. – Greg Jan 10 '11 at 22:07
0

In the website, when the application starts (Application_Start in the global.asax), you can pass a reference of the membership provider that the website is aware of to the DLL you've written. The dll will simply expect the type System.Web.Security.MembershipProvider. This way the dll can still invoke the needed method from the implementation, but not know the runtime type of the provider at compile time (this is what is known as Dependency Injection).

Brian Ball
  • 12,268
  • 3
  • 40
  • 51
  • Because of our current architecture, the web site is not always "running". We have several DLLs that provide info via WebOrb to our widgets on other people's websites. I am currently writing a customized set of Providers where I can pass in the config information. So far this looks the most promising. – Keith Barrows Dec 30 '10 at 16:47