具有 MVC3、ASP.NET 成员资格的多用户应用程序 - 用户身份验证/数据分离
我正在使用 ASP.NET MVC3 和 EF4、一个数据库、一个代码库构建一个简单的多用户(多租户?)应用程序,所有用户都使用相同的 URL 访问该应用程序。用户登录后,他们应该只能访问自己的数据,我使用默认的 asp.NET 成员资格提供程序,并在每个数据表上添加了一个“UserId”Guid 字段。显然,我不希望用户 A 能够访问用户 B 的数据,因此我将以下内容添加到控制器上的几乎每个操作中。
public ActionResult EditStatus(int id)
{
if (!Request.IsAuthenticated)
return RedirectToAction("Index", "Home");
var status = sService.GetStatusById(id);
// check if the logged in user has access to this status
if (status.UserId != GetUserId())
return RedirectToAction("Index", "Home");
.
.
.
}
private Guid GetUserId()
{
if (Membership.GetUser() != null)
{
MembershipUser member = Membership.GetUser();
Guid id = new Guid(member.ProviderUserKey.ToString());
return id;
}
return Guid.Empty;
}
这种重复肯定感觉不对,必须有一种更优雅的方法来确保我的用户无法访问彼此的数据 - 我错过了什么?
I'm building a simple multi-user (multi-tenant?) App with ASP.NET MVC3 and EF4, one database, one code base, all users access the app using the same URL. Once a User is logged in they should only have access to their data, I'm using the default asp.NET membership provider and have added a ‘UserId’ Guid field on each of the data tables. Obviously I don't want user A to have any access to user B’s data so I have been adding the following to nearly every action on my controllers.
public ActionResult EditStatus(int id)
{
if (!Request.IsAuthenticated)
return RedirectToAction("Index", "Home");
var status = sService.GetStatusById(id);
// check if the logged in user has access to this status
if (status.UserId != GetUserId())
return RedirectToAction("Index", "Home");
.
.
.
}
private Guid GetUserId()
{
if (Membership.GetUser() != null)
{
MembershipUser member = Membership.GetUser();
Guid id = new Guid(member.ProviderUserKey.ToString());
return id;
}
return Guid.Empty;
}
This repetition is definitely feeling wrong and there must be a more elegant way of ensuring my users can't access each other's data – what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自定义模型绑定器:
然后您将在
Application_Start
中注册此模型绑定器:最后
结论:我们已经将此控制器放在了节食上,这就是控制器应有的方式:-)
A custom model binder:
and then you would register this model binder in
Application_Start
:and finally
Conclusion: We've put this controller on a diet which is the way controllers should be :-)
试图弄清楚这个实现(我有完全相同的问题),我发现了 Scott Hanselman 的文章
的 MVC 菜鸟来说,这更容易理解。
Trying to get my head around this implementation (I'm having exactly the same question), I found a similar approach described in Scott Hanselman't post
http://www.hanselman.com/blog/IPrincipalUserModelBinderInASPNETMVCForEasierTesting.aspx
For a total MVC noob as myself, that was somewhat easier to understand.