且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在dll中使用asp.net会员提供商

更新时间:2022-12-10 12:09:16

嗯,看来,设置SqlMembershipProvider的连接字符串的唯一方法是使用初始化方法,根据文档,不应该从我们的代码中调用。 / p>


使用ASP.NET应用程序的
中指定的属性值
初始化SQL Server成员资格
提供程序配置文件这个方法不是直接从
代码使用的


所以基本上我们必须一个方法来将该信息导入到与托管该DLL的应用程序相关联的配置文件中。



您所要做的只是参考系统。在程序集中的web.dll system.configuration.dll 中,使用SqlMembershipProvider,然后在app.config中设置适当的值以执行像这样:

 <?xml version =1.0encoding =utf-8?> 
< configuration>
< connectionStrings>
< add name =MembershipConnectionStringconnectionString =connectionstringdetailsproviderName =System.Data.SqlClient/>
< / connectionStrings>
< system.web>
< membership defaultProvider =DefaultSqlMembershipProvider>
< providers>
< clear />
< add name =DefaultSqlMembershipProviderconnectionStringName =MembershipConnectionStringtype =System.Web.Security.SqlMembershipProvider/>
< / providers>
< / membership>
< /system.web>
< / configuration>

需要注意的是,如果您使用MyAssembly.dll和TheApp.exe这应该是TheApp.exe.config,而不是MyAssembly.dll.config。配置文件始终与执行程序集相关联。


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

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.