自定义 ASP.NET SqlMembershipProvider - 处理连接字符串

发布于 2024-09-24 01:23:04 字数 596 浏览 1 评论 0原文

我正在创建一个自定义 SqlMembershipProvider 类,以向基类添加一些增强功能。不过,我正在忙于处理连接字符串。如何从配置中读取连接字符串名称并使其可供其余方法使用?

现在我有:

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

            _ConnectionStringName = config["connectionStringName"];
        }

但在其他方法中,_ConnectionStringName 变量为 null:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[_ConnectionStringName].ConnectionString)

存储连接字符串名称以便在我的自定义成员资格提供程序中全局可用的正确方法是什么?

谢谢!

I am creating a custom SqlMembershipProvider class to add some enhanced functionality to the base class. I'm getting caught up on handling the connection string, though. How can I read the Connection String Name from the configuration and make that available to the rest of the methods?

Right now I have:

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

            _ConnectionStringName = config["connectionStringName"];
        }

But in other methods, the _ConnectionStringName variable is null:

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[_ConnectionStringName].ConnectionString)

What is the proper way to store the Connection String Name so it is available globally in my custom membership provider?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠意 2024-10-01 01:23:04

如果在获取配置集合时还剩下任何条目,则 ProviderBase 会抛出 ConfigurationException ,因此每个提供程序在调用 base.Initialize 之前都会删除其配置条目

正如您所发现的 这个答案是你必须在调用base.Initialize之前获取你的值。

抱歉,我第一眼就错过了。


这篇文章的其余部分是历史性的,虽然技术上正确,但忽略了上面列举的突出问题。

首先 - 尝试 WebConfigurationManager.ConnectionStrings

WebConfigurationManager 负责应用 web.config 的层次结构,从 windows\microsoft.net\framework\2.0xxxx\web.config 一直到您的应用程序。

此行为在 ConfigurationManager 中不存在,它通常处理 machine.config 到 app.config。


如果这不能解决您的问题,如果 _ConnectionStringName 确实在 Initialize 中正确分配,则您必须覆盖代码中其他位置的值。

首先,设置断点并确保 _ConnectionStringName 按预期设置。

然后找到对该字段的所有引用并确保没有错误。

当然,这是假设 _ConnectionStringName 是私有字段。如果不是,请使其如此并查找编译错误。

ProviderBase will throw a ConfigurationException if there are any entries left in the config collection by the time it get's it so each provider removes it's configuration entries before calling base.Initialize.

The issue, as you have found as a result of this answer is that you must get your values before calling base.Initialize.

Sorry, I missed that at first glance.


The rest of this post is historical and while technically correct misses the salient issue here as enumerated above.

First - try WebConfigurationManager.ConnectionStrings.

WebConfigurationManager handles applying the hierarchy of web.config all the way from your windows\microsoft.net\framework\2.0xxxx\web.config all the way up to your app.

This behaviour is not present in ConfigurationManager, which typically deals with machine.config to app.config.


If this does not solve your problem you must be overwriting the value elsewhere in your code, if indeed _ConnectionStringName is being properly assigned in Initialize.

First, set a breakpoint and ensure that _ConnectionStringName is being set as expected.

Then locate all references to the field and ensure that you do not have a bug.

This is assuming, of course, that _ConnectionStringName is a private field. If it is not, make it so and look for your compile error.

愚人国度 2024-10-01 01:23:04

不确定这是否有帮助,但我遇到了类似的问题,需要重写 SqlMembershipProvider 子类中的连接字符串。

这个想法不是我自己的 - 我在这个论坛帖子的评论部分找到了它:
http://forums.asp.net/p/997608/2209437.aspx

public override void Initialize(string name, NameValueCollection config)
{
     base.Initialize(name, config);<br>
     string connectionString =  //...what you want your connection string to be, 
                                //so config["connectionStringName"]...
     // Set private property of Membership provider.
     System.Reflection.FieldInfo connectionStringField = 
         GetType().BaseType.GetField("_sqlConnectionString", 
         System.Reflection.BindingFlags.Instance 
         | System.Reflection.BindingFlags.NonPublic);

     connectionStringField.SetValue(this, connectionString);
}

我很抱歉 - 我以前从未在这里发过帖子,所以格式可能低于标准!

Not sure if this helps, but I was having a similar issue in needing to override the connectionstring in a sub-class of SqlMembershipProvider.

This idea is not my own - I found it in the comments section of this forum posting:
http://forums.asp.net/p/997608/2209437.aspx

public override void Initialize(string name, NameValueCollection config)
{
     base.Initialize(name, config);<br>
     string connectionString =  //...what you want your connection string to be, 
                                //so config["connectionStringName"]...
     // Set private property of Membership provider.
     System.Reflection.FieldInfo connectionStringField = 
         GetType().BaseType.GetField("_sqlConnectionString", 
         System.Reflection.BindingFlags.Instance 
         | System.Reflection.BindingFlags.NonPublic);

     connectionStringField.SetValue(this, connectionString);
}

My apologies - I've never posted here before, so the formatting may be sub-par!

等风来 2024-10-01 01:23:04

这可能已经晚了 6 个月,无法提供帮助,但我能够通过这种方式获取连接字符串:

using System.Web.Configuration;

Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
MembershipSection section = config.SectionGroups["system.web"].Sections["membership"] as MembershipSection;
string defaultProvider = section.DefaultProvider;
string connstringName = section.Providers[defaultProvider].ElementInformation.Properties["connectionStringName"].Value.ToString();
string val = config.ConnectionStrings.ConnectionStrings[connstringName].ConnectionString;

这假设默认提供程序具有连接字符串属性 - 但如果您要子类化 SqlMembershipProvider那么应该总是有一个,位于 web.config 链的某个位置(我相信它是在 machine.config 中定义的)。

所有这一切,都要添加一种方法 change用户名

This is probably 6 months too late to help, but I was able to get the connection string this way:

using System.Web.Configuration;

Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
MembershipSection section = config.SectionGroups["system.web"].Sections["membership"] as MembershipSection;
string defaultProvider = section.DefaultProvider;
string connstringName = section.Providers[defaultProvider].ElementInformation.Properties["connectionStringName"].Value.ToString();
string val = config.ConnectionStrings.ConnectionStrings[connstringName].ConnectionString;

This makes the assumption that the default provider has a connection string property - but if you're subclassing SqlMembershipProvider then should always have one, somewhere up the web.config chain (it's defined in the machine.config I believe).

All of this, to add one method to change the username.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文