且构网

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

更改成员资格连接字符串

更新时间:2023-11-07 22:02:58

我在网上也发现有关问题的原因是此处:

What i have also found on the net about problem is this here:

一个更简单的方法,尽管有些令人惊叹,但它只是在修改 提供者中的连接字符串应尽早在请求的 生命周期:

A simpler, albeit somewhat eyebrow-raising solution is just modifying the connection string in the providers early enough in the request's lifecycle:

     private void SetProviderConnectionString(string connectionString)
     {
         var connectionStringField = 
         Membership.Provider.GetType().GetField("_sqlConnectionString", 
                     BindingFlags.Instance | BindingFlags.NonPublic);

         if (connectionStringField != null)
             connectionStringField.SetValue(Membership.Provider, connectionString);
     }

从Global.asax.cs内部调用此方法 Application_PreRequestHandlerExecute可以完成这项工作.还没测试 太多了,但是即使某事不起作用,也仅表示它需要 早些做.不保证它将在将来的版本中使用 框架,尽管很有可能.

Calling this method from Global.asax.cs inside Application_PreRequestHandlerExecute does the job. Haven't tested it too much, but even if something doesn't work, it just means it needs to be done earlier. No guarantees this will work with future versions of the framework, although most likely it will.

因此,可以(在Initialize方法完成之后)手动调用"SetProviderConnectionString"方法,而不是期望框架在首次引用Membership.Provider时调用覆盖的Initialize方法.

So, the 'SetProviderConnectionString' method can be called manually (after the Initialize method is finished), instead of expecting the framework to call the overwritten Initialize method when the Membership.Provider is referenced for a first time.