自定义 ASP.NET SqlMembershipProvider - 处理连接字符串
我正在创建一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果在获取配置集合时还剩下任何条目,则 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 aConfigurationException
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 callingbase.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 inInitialize
.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.不确定这是否有帮助,但我遇到了类似的问题,需要重写 SqlMembershipProvider 子类中的连接字符串。
这个想法不是我自己的 - 我在这个论坛帖子的评论部分找到了它:
http://forums.asp.net/p/997608/2209437.aspx
我很抱歉 - 我以前从未在这里发过帖子,所以格式可能低于标准!
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
My apologies - I've never posted here before, so the formatting may be sub-par!
这可能已经晚了 6 个月,无法提供帮助,但我能够通过这种方式获取连接字符串:
这假设默认提供程序具有连接字符串属性 - 但如果您要子类化
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:
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.