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

We are using ASP.Net Membership Providers (SQL Server Provider), and I want to be able to configure an administrator user account as part of our installer. To do this, I need the ASP.Net Membership Provider to configure it so that my installer can use it, but I do not want to configure the configuration file for the installer.

So, is there a way to configure ASP.Net membership through code without writing a custom provider?

+6
c # windows-installer asp.net-membership
source share
2 answers

Well, after the solution they called here: http://forums.asp.net/p/997608/2209437.aspx

I created a class that in the constructor without parameters (which you need) just gets the server and port from the command line arguments. So I can go "MemberhipInitializer.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 app.config of your console application (or windows application)

 <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 also need the role provider bit, you may have to override SqlRoleProvider in much the same way.

If you create a program to automatically initialize your membership database in a specific state, but the server address and sql port name are unknown until someone enters them into the installation wizard, this will do the trick.

Does it help?

+7
source share

Membership.ApplicationName = "yourfbaApplicationame"; MembershipUser user = Membership.CreateUser("admin,"password"," emaiol@g.com ");

and make sure you have the following entries in the configuration file.

  <connectionStrings> <add connectionString="server=sqd01-1-cll;database=FBA;Integrated Security=SSPI;" name="FBASqlConnString" providerName="System.Data.SqlClient"/> 

If you still want to use the full code and should not use the configuration file. Use below

 SqlMembershipProvider ObjSqlMembershipProvider = new SqlMembershipProvider(); SqlRoleProvider ObjSqlRoleProvider = new SqlRoleProvider(); NameValueCollection ObjNameValueCollRole = new NameValueCollection(); NameValueCollection ObjNameValueCollMembership = new NameValueCollection(); MembershipCreateStatus enMembershipCreateStatus; ObjNameValueCollMembership.Add("connectionStringName", "Connection String Name"); ObjNameValueCollMembership.Add("applicationName", "ApplicatioNAme"); //these items are assumed to be Default and dont care..Should be given a look later stage. ObjNameValueCollMembership.Add("enablePasswordRetrieval", "false"); ObjNameValueCollMembership.Add("enablePasswordReset", "false"); ObjNameValueCollMembership.Add("requiresQuestionAndAnswer", "false"); ObjNameValueCollMembership.Add("requiresUniqueEmail", "false"); ObjNameValueCollMembership.Add("passwordFormat", "Hashed"); ObjNameValueCollMembership.Add("maxInvalidPasswordAttempts", "5"); ObjNameValueCollMembership.Add("minRequiredPasswordLength", "1"); ObjNameValueCollMembership.Add("minRequiredNonalphanumericCharacters", "0"); ObjNameValueCollMembership.Add("passwordAttemptWindow", "10"); ObjNameValueCollMembership.Add("passwordStrengthRegularExpression", ""); //hard coded the Provider Name,This function just need one that is present. I tried other names and it throws error. I found this using Reflector ..all the rest are take care by the above //name value pairs ObjSqlMembershipProvider.Initialize("AspNetSqlMembershipProvider", ObjNameValueCollMembership);MembershipUser user = ObjSqlMembershipProvider.CreateUser("admin,"password"," emaiol@g.com "); 

This is necessary so that the connection string in the configuration file is no good. If you want this to be from code too, you need to inherit the class

-one
source share

All Articles