从代码动态设置成员资格的连接字符串

时间:2012-10-11 11:38:50

标签: c# connection-string membership

我有一个.net网络应用程序,它使用会员资格进行用户验证。成员资格在web.config文件中有一个定义,并引用文件中的连接字符串(web.config),我需要在web.config中动态地设置代码的成员关系的连接字符串不是静态的。

我该怎么做?

提前致谢。

2 个答案:

答案 0 :(得分:5)

以下C#示例代码演示了如何configure a .NET membership provider programmatically Jacques L. Chereau。此代码还要求您configure a connection string命名为MyDatabase

NameValueCollection objConfig = new NameValueCollection();
objConfig.Add("connectionStringName", "MyDatabase");
objConfig.Add("enablePasswordRetrieval", "false");
objConfig.Add("enablePasswordReset", "true");
objConfig.Add("requiresQuestionAndAnswer", "true");
objConfig.Add("applicationName", "MyApp");
objConfig.Add("requiresUniqueEmail", "true");
objConfig.Add("maxInvalidPasswordAttempts", "5");
objConfig.Add("passwordAttemptWindow", "10");
objConfig.Add("commandTimeout", "30");
objConfig.Add("passwordFormat", "Hashed");
objConfig.Add("name", "AspNetSqlMembershipProvider");
objConfig.Add("minRequiredPasswordLength", "8");
objConfig.Add("minRequiredNonalphanumericCharacters", "2");
objConfig.Add("passwordStrengthRegularExpression", "(?=^.{8,25}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+}{\\":;'?/>.<,])(?!.*\\s).*$"));

SqlMembershipProvider objSqlMembershipProvider = new SqlMembershipProvider();
objSqlMembershipProvider.Initialize(objConfig["name"], objConfig);
MembershipProviderCollection colMembershipProviders = new MembershipProviderCollection();
colMembershipProviders.Add(objSqlMembershipProvider);
colMembershipProviders.SetReadOnly();

BindingFlags enuBindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
Type objMembershipType = typeof(Membership);
objMembershipType.GetField("s_Initialized", enuBindingFlags).SetValue(null, true);
objMembershipType.GetField("s_InitializeException", enuBindingFlags).SetValue(null, null);
objMembershipType.GetField("s_HashAlgorithmType", enuBindingFlags).SetValue(null, "SHA1");
objMembershipType.GetField("s_HashAlgorithmFromConfig", enuBindingFlags).SetValue(null, false);
objMembershipType.GetField("s_UserIsOnlineTimeWindow", enuBindingFlags).SetValue(null, 15);
objMembershipType.GetField("s_Provider", enuBindingFlags).SetValue(null, objSqlMembershipProvider);
objMembershipType.GetField("s_Providers", enuBindingFlags).SetValue(null, colMembershipProviders);

假设您有以下库引用:

using System.Web.Security;
using System.Collections.Specialized;
using System.Reflection;

修改

此方法在请求的生命周期

中尽早设置成员资格提供程序中的连接字符串
private void SetMembershipProviderConnectionString(string connectionString)
{
   // Set private property of Membership. Untested code!!
   var connectionStringField = Membership.Provider.GetType().GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
   if (connectionStringField != null)
      connectionStringField.SetValue(Membership.Provider, connectionString);

}

未经测试但是从Application_PreRequestHandlerExecute内的Global.asax.cs调用此方法可以完成这项工作。

答案 1 :(得分:2)

有一个更简单的解决方案。

  1. 创建一个扩展SqlMembershipProvider类的子类。
  2. 覆盖初始化方法
  3. 设置connectionString属性config [“connectionString”]
  4. 让base.Initialize logic继续
  5. 在web.config文件中,使用您的。而不是SqlMembershipProvider 自定义类。 (如果你给它一个不同的名字,请确保设置defaultProvider属性以匹配它)