您的位置:首页 > 其它

【译】MVC3 20个秘方-(1)用密码保护限制对view的访问

2011-11-22 15:06 267 查看
场景
你想阻止用户访问你网站的特定页面,除非用户已经注册并且使用了用户名和密码登陆。
<!--[if !supportLineBreakNewLine]-->

解决方案

使用一个AccountController,AccountModels 和 几个MVC View,配合ASP.NET的 AuthorizeAttribute 特性,FormsAuthentication和Membership creation/validation

讨论

微软的MVC团队已经对账户controller做了很多的改进。它已经被更新用于Form验证,连同Membership 类去创建新的用户,验证存在的用户,创建cookie去检测用户登入的状态。

在MVC 3中 已经提供了几种默认的应用程序模板。如下图。

View Code

// Retrieve a list of all users to allow an admin
// to manage them
[Authorize(Roles = "Admin")]
public ActionResult UserAdmin()
{
MembershipUserCollection users =
Membership.GetAllUsers();
return View(users);
}
// Create some custom reports for me only
[Authorize(Users = "Jamie")]
public ActionResult JamieAdmin()
{
// Perform some logic to generate usage reports
...
return View();
}


以上简单的例子仅仅是怎样限制访问内容的开始。

另请参阅

AuthorizeAttribute, FormsAuthentication, and Membership

<!--[endif]-->
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐