问题描述
我正在创建使用单独的数据块为每个租户并已试图获得一张会员为每个租户没有任何运气要工作MVC4多租户的网站。
由于某些原因connectionStringField总是空,无论我做什么。任何想法,为什么会发生这种情况?
公共类codeFirstMembershipProvider:的MembershipProvider
{
公共覆盖无效初始化(字符串名称,System.Collections.Specialized.NameValueCollection配置)
{
base.Initialize(名称,配置); //硬$ C $光盘进行测试
字符串的connectionString = @服务器=本地主机;数据库= masterdb; UID =主; PWD =密码; //会员提供集私有财产。
字段信息connectionStringField =的GetType()BaseType.GetField(_ sqlConnectionString,BindingFlags.Instance | BindingFlags.NonPublic可)。
如果(connectionStringField!= NULL)
connectionStringField.SetValue(这一点,的connectionString);
}
}
您不能使这项工作。即使你能弄清楚如何获得的反射功能,您想要的方式。这将导致不稳定和不可预料的结果。
的原因是,该成员资格提供]是在所有并发用户使用的全局静态的,单个实例。这意味着如果你更改连接字符串为一个用户,它改变了它所有的人。更糟的是,你可以改变它的,然后有一个上下文切换到另一个用户,他们可能会覆盖这种变化,然后回来和应用程序不在状态,它认为它在
内置的会员制的目的不是要把它运行时,它改变了内部数据。它的设计以使用单个数据库多种应用
如果你坚持要用不同的数据库为每个租户,那么你将不得不实现自己的认证系统,因为成员根本就不会在你想要的情况下工作。它可能会出现在第一个工作,但因为你的网站获得更多的并发用户,这是一个定时炸弹等待你的脸炸毁。
I am working on creating a MVC4 multi-tenant site that uses seperate DBs for each tenant and have been trying to get the membership piece to work for each tenant without any luck.
For some reason connectionStringField is always null no matter what I do. Any ideas why this could happen?
public class CodeFirstMembershipProvider : MembershipProvider
{
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
// hard coded for testing
string connectionString = @"server=localhost;database=masterdb;uid=master;pwd=password";
// Set private property of Membership provider.
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
if(connectionStringField != null)
connectionStringField.SetValue(this, connectionString);
}
}
You can't make this work. Even if you could figure out how to get the reflection to function the way you want. It will lead to instability and unexpected results.
The reason is that the Membership Provider is a global static, single instance that is used across all concurrent users. That means if you change the connection string for one user, it changes it for all of them. Worse, you might change it for one, then have a context switch to another user and they might overwrite that change, then come back and the app isn't in a state that it thinks it's in.
The built-in Membership system is not designed to have it's internal data changed while it is running. It's designed to use a single database for multiple applications.
If you insist on using separate databases for each tenant, then you will have to implement your own authentication system, because Membership simply will not work in the situation you want it to. It may appear to work at first, but as your site gains more concurrent users, it's a ticking time bomb waiting to blow up in your face.
这篇关于ASP.NET MVC 4和codeFirst动态变化的成员连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!