更改成员资格连接字符串 [英] Changing membership connection string

查看:167
本文介绍了更改成员资格连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp.net成员资格的新手&我需要以编程方式更改其连接字符串的帮助.

Iam new to asp.net membership & I need help to change its connection string programmatically.

到目前为止,我一直在尝试

What I have tried till now is

我已经创建了一个类项目名称Sample作为命名空间**,并扩展了System.Web.Security.SqlMembershipProvider

I have create a class project name Sample as namespace** and extends the System.Web.Security.SqlMembershipProvider

编码为

namespace Sample
{
    public class Connectionstring : SqlMembershipProvider
    {
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            string connectionString = "server=xx.xx.xx;database=db;user id=un;password=pwd";    

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

,并将成员身份标签中的Web配置文件更改为

and altered web config file in membership tag as

<membership defaultProvider="SQLMembershipProvider">
  <providers>
    <add name="SQLMembershipProvider" type="sample.Connectionstring,sample" connectionStringName="SQLMembershipConnString" applicationName="@@@@@@@" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" />
  </providers>
</membership>

在运行Web应用程序项目时,我正在更改的连接字符串不会更改吗?

and while running the web application project the connection string which i am changing is not get altered?

等待您的宝贵反馈和意见

waiting for your valuable responses and comments

推荐答案

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

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.

这篇关于更改成员资格连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆