简体   繁体   English

ASP.NET MVC 4多租户

[英]ASP.NET MVC 4 multi tenancy

In ASP.NET MVC 4 I see that there is and [Authorize] attribute and now a corresponding [AllowAnonymous] attribute that can easily let you require authentication to access specific controller actions. 在ASP.NET MVC 4中,我看到有[Authorize]属性,现在有一个对应的[AllowAnonymous]属性,可以轻松地要求您进行身份验证以访问特定的控制器操作。

What I need is true multi tenancy though. 我需要的是真正的多租户。 Each user can access only their own records, and all records other than the user accounts should be owned by individual users. 每个用户只能访问自己的记录,除用户帐户外的所有记录应归单个用户所有。

In Hobo (http://hobocentral.net) which is a Rails plugin, this was easily accomplished by adding the following line of code in my ApplicationController: 在作为Rails插件的Hobo(http://hobocentral.net)中,可以通过在我的ApplicationController中添加以下代码行来轻松实现此目的:

before_filter :login_required, :except => [:login, :signup, :do_signup, :activate]

And then in my model: 然后在我的模型中:

belongs_to :owner, :class_name => "User", :creator => true

# --- Permissions --- #

def create_permitted?
  acting_user == owner || !owner_changed?
end

def update_permitted?
  acting_user == owner || !owner_changed?
end

def destroy_permitted?
  acting_user == owner || !owner_changed?
end

def view_permitted?(field)
  owner_is? acting_user or new_record?
end

And finally in my model's controller: 最后在我模型的控制器中:

def index
  hobo_index current_user.modelName
end

Does something so simple and elegant exist or is built into ASP.NET MVC? ASP.NET MVC是否存在如此简单而精致的功能? So far I've found several ways to implement multi tenancy in ASP.NET MVC but I'm unsure as to which is the clearly correct way. 到目前为止,我已经找到了几种在ASP.NET MVC中实现多租户的方法,但是我不确定哪种方法是正确的。 I also intent to use .NET 4.5 and Entity Framework 5 if that helps. 如果有帮助,我也打算使用.NET 4.5和Entity Framework 5。

if you are using any type of built in authentication in asp.net MVC then its already present as you can use something like 如果您在asp.net MVC中使用任何类型的内置身份验证,那么它已经存在,您可以使用类似

HttpContext.Current.User.Identity.Name

and if you are not using some kind of internal authentication mechanism then you can do what i did simple when authentication a user save the primary key in a session variable. 如果您不使用某种内部身份验证机制,则可以执行身份验证时将用户将主键保存在session变量中的简单操作。

Session["User"] = Key;

and inside each controller take out the variable 然后在每个控制器内部取出变量

var key = Session["User"];

and retrive the user data based on the key 并根据密钥检索用户数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM