我正在看MVC帐户控制器,它似乎来自ASP.NET webforms.有关于如何使用它的任何好的背景信息吗?
您可以将其映射到用户数据库表,还是更好地推广自己的用户管理?
如何在MVC中使用它来限制登录用户可以查看的页面?你必须自己完成所有这些吗?
Web上的哪些资源有助于理解ASP.NET成员资格?
han*_*ngy 18
我正在看MVC帐户控制器....它似乎来自asp.net?
Scott Guthrie在关于ASP.NET MVC Preview 4的博客文章中对此做了很好的解释.他基本上说MVC示例中的Account Controller使用ASP.NET成员资格提供程序,因此您可以使用其中的任何一个.(我认为您可以在Internet上找到有关ASP.NET成员资格提供程序的更多信息.)如果您不想实现/使用其中之一,那么修改应用程序以使用您自己的用户管理可能是最佳选择.
如何在MVC中使用它来限制登录用户可以查看的页面?你必须自己完成所有这些吗?
您可以将Authorize属性添加到控制器类或操作方法.(同源如上.)
// 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");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14193 次 |
| 最近记录: |