开发者

using asp.net membership provider in a dll

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开发者_如何学C 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


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜