我正在查看 MVC 帐户控制器,它似乎来自 ASP.NET Webforms。有没有关于如何使用它的好的背景信息?
您可以将其映射到用户数据库表还是最好进行自己的用户管理?
如何在 MVC 中使用它来限制登录用户可以查看的页面?您必须自己完成所有这些工作吗?
Web 上的哪些资源可以帮助您了解 ASP.NET 会员资格?
我正在查看 MVC 帐户
控制器......它似乎来自
ASP.NET?
Scott Guthrie 在他的博客文章中很好地解释了这一点ASP.NET MVC 预览版 4 http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx。他基本上说 MVC 示例中的帐户控制器使用 ASP.NET 成员资格提供程序,因此您可以使用其中任何一个。 (我认为您可以在 Internet 上找到有关 ASP.NET 成员资格提供程序的更多信息。)如果您不想实现/使用其中之一,则修改应用程序以使用您自己的用户管理可能是最好的选择。
你如何在 MVC 中使用它
限制登录用户的页面
可以查看吗?你必须滚动所有
那是你自己的吗?
您可以添加Authorize
控制器类或操作方法的属性。 (相同的source http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-preview-4-release-part-1.aspx如上。)
// 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="COMPANY\\joed")]
public ActionResult ChangeId(int oldId, int newId)
{
this.stuffRepository.ChangeId(oldId, newId);
return RedirectToAction("Index");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)