Well, it seems that the only way to set the SqlMembershipProvider connection string is to use the Initialize method, which according to the documentation should not be called from our code.
Initializes membership in the SQL Server provider with the property values โโspecified in the ASP.NET application configuration file. This method is not intended to be used directly.
So, basically, we need to figure out a way to get this information in the configuration file associated with the application in which the dll is located.
All you have to do is reference the system.web.dll and system.configuration.dll in your assembly, use SqlMembershipProvider and then set the correct values โโin app.config for the executable as follows:
<?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>
It is important to note that if you are "MyAssembly.dll" and "TheApp.exe", it should be "TheApp.exe.config" and not "MyAssembly.dll.config". The configuration file is always associated with the running assembly.
source share