Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure ASP.Net membership providers through code?

We're using ASP.Net membership providers (the SQL Server provider), and I want to be able to set up an Admin user account as part of our installer. To do this, I need the ASP.Net membership provider configured so that my installer can use it - but I don't want to have to set up a config file for the installer.

So is there a way of configuring an ASP.Net membership through code without writing a custom provider?

like image 407
Samuel Jack Avatar asked Jun 22 '09 08:06

Samuel Jack


1 Answers

Ok, following the solution they conjured up here: http://forums.asp.net/p/997608/2209437.aspx

I created a class which, in the parameterless constructor (which you need) just gets the server and port from the command line arguments. Such that I can go "MembershipInitializer.exe "SomeSqlServer\Instance" 51000.

public class CustomSQLMembershipProvider : SqlMembershipProvider {
  private readonly string _server;
  private readonly string _port;

  /// <summary>
  /// Initializes a new instance of the <see cref="T:System.Web.Security.SqlMembershipProvider"/> class.
  /// </summary>
  public CustomSQLMembershipProvider() {
    string[] args = System.Environment.GetCommandLineArgs();
    // args[0] is the exe name
    _server = args[1];
    _port = args[2];
  }

  public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
  {
    base.Initialize(name, config);

    // Update the private connection string field in the base class.
    string connectionString = string.Format(@"Data Source={0},{1};Initial Catalog=aspnetdb;UID=NICCAMembership;PWD=_Password1;Application Name=NICCA;Connect Timeout=120;", _server, _port);

    // Set private property of Membership provider.
    FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
    connectionStringField.SetValue(this, connectionString);
  }
}

In the app.config of your console app (or windows app)

<configuration>
 <connectionStrings>
  <clear/>
  <add name="MembershipDB"
    connectionString="Some Bogus String here that gets overrided in the custom class" 
    providerName="System.Data.SqlClient"/>
 </connectionStrings>
 <system.web>
  <authentication mode="Forms"/>
  <authorization>
   <deny users="?"/>
  </authorization>
  <membership>
   <providers>
    <remove name="AspNetSqlMembershipProvider"/>
    <add name="AspNetSqlMembershipProvider" 
      connectionStringName="MembershipDB" 
      applicationName="NICCA" 
      type="MyInitializeMembershipDB.CustomSQLMembershipProvider, MyInitializeMembershipDB" 
      requiresUniqueEmail="false" 
      requiresQuestionAndAnswer="false"/>
   </providers>
  </membership>
 </system.web>
</configuration>

If you are in need of the role provider bit as well, you probably have to override SqlRoleProvider in much the same way.

If you are creating a program to automatically initialize your membership database to a specific state based but the sql server address and port name aren't known until someone enters them into a install wizard, this will do the trick..

Does this help?

like image 65
cbeuker Avatar answered Oct 16 '22 14:10

cbeuker