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");
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-core – .NET Core的静态代码分析工具
- asp.net-mvc-4 – 捆绑从CDN提供的多个CSS?
- MONO / ASP.NET Linux主机?
- asp.net-mvc – 如何使用Ninject将服务注入授权过滤器?
- asp.net-mvc-4 – 如何在Kendo Grid的每一行中添加自定义按
- asp.net – LINQ:列表中唯一项目的计数
- asp.net – 我应该使用WebMatrix构建一个真实世界的网站吗?
- asp.net-mvc – 使用ASP.NET MVC测试驱动的开发 – 从哪里开
- 转:[WebServices]介绍
- asp.net – 如何将IIS Developer Express切换到“经典模式”
- asp.net-mvc – 如何为mvc应用程序中的所有控制器
- asp.net-mvc – Razor视图与部分视图
- asp.net-mvc-3 – MVC3页面 – IsPostback功能
- asp.net-mvc – Actionresult vs JSONresult
- 如何在子文件夹中托管ASP.NET MVC站点
- asp.net-mvc – 如何在View中获取当前的url值
- 如何从asp.net中的javascript调用codebehind函数
- asp.net-mvc-4 – 如何在asp.net MVC4查看页面中
- ASP.NET 1.1到4.0迁移:事件不工作
- asp.net-mvc – 如何将应用程序用户放在与其余对
