asp.net-mvc – ASP.NET MVC帐户控制器使用指南?
我正在看MVC账户控制器,似乎来自于ASP.NET Webforms.有没有什么好的背景信息如何使用它?
您可以将其映射到用户数据库表,还是更好地滚动您自己的用户管理? 如何在MVC中使用它来限制登录用户可以查看的页面?你必须自己滚动所有这些吗? 网络上的哪些资源可以帮助您了解ASP.NET会员资格? 解决方法
Scott Guthrie在他关于ASP.NET MVC Preview 4的博客条目中解释得很好.他基本上说MVC示例中的Account Controller使用ASP.NET成员资格提供者,因此可以使用其中的任何一个. (我想你可以在互联网上找到关于ASP.NET会员提供商的更多信息.)如果您不想实现/使用其中之一,修改应用程序以使用您自己的用户管理可能是最佳选择.
您可以将Authorize属性添加到控制器类或操作方法. (同上source) // Only logged in users can access this controller. [Authorize] public class SomeController : Controller { #region Not really important for this example. :] // Maybe rather use a BLL service here instead of the repository from the DAL,but this example is already more verbose than required. private IStuffRepository stuffRepository; public SomeController(IStuffRepository stuffRepository) { if (null == stuffRepository) { throw new ArgumentNullException("stuffRepository"); } this.stuffRepository = stuffRepository; } #endregion // The authorize attribute is inherited - only logged in users can use the index action. public ActionResult Index() { return View(); } // Moderators can flag stuff. [Authorize(Roles="Moderator")] public ActionResult Flag(int id) { this.stuffRepository.Flag(id); return RedirectToAction("Index"); } // Admins ans SysOps can delete stuff. [Authorize(Roles="Admin,SysOp")] public ActionResult Delete(int id) { this.stuffRepository.Delete(id); return RedirectToAction("Index"); } // Only joed can change the objects stuff. ;) // (This is probably bullshit,of course,but I could not make any better example. I blame the fact it is late at night. :)) [Authorize(Users="COMPANYjoed")] public ActionResult ChangeId(int oldId,int newId) { this.stuffRepository.ChangeId(oldId,newId); return RedirectToAction("Index"); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- entity-framework – 在ViewBag中填充匿名类型,导致模型绑定
- asp.net – 跨应用程序的表单身份验证
- asp.net – 以编程方式从GridView添加和删除事件
- asp.net-mvc – ASP .NET MVC正确的UserControl架构
- asp.net-mvc – 纯粹的DDD方法可以和NHibernate一起使用吗?
- asp.net-mvc – 使用AWS .NET SDK进行SNS订阅确认示例
- asp.net-mvc – MVC3中的OutputCache和Authorize过滤器
- asp.net – 为什么在IIS已经处理请求并发时使用异步控制器?
- asp.net – 什么是连接池?
- asp.net – ASP .Net VNext和Owin
- attributes – 利用属性版本控制在Swagger中利用
- asp.net-mvc – 使用Html.ActionLink传递文本框值
- asp.net – 使用Web服务导致“无法处理没有有效操
- asp.net-mvc – 在Visual Studio中使用TypeScrip
- asp.net-mvc – asp.net mvc – 子文件夹
- asp.net-mvc – 在控制器名称和路由中使用破折号
- asp.net – 是否有可能欺骗或重用VIEWSTATE或检测
- asp.net – Global.asax PostAuthenticateReques
- ASP.Net注销代码块
- asp.net-mvc-3 – 在LINQ Query中调用一个方法