加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.NET MVC帐户控制器使用指南?

发布时间:2020-12-15 19:04:03 所属栏目:asp.Net 来源:网络整理
导读:我正在看MVC账户控制器,似乎来自于ASP.NET Webforms.有没有什么好的背景信息如何使用它? 您可以将其映射到用户数据库表,还是更好地滚动您自己的用户管理? 如何在MVC中使用它来限制登录用户可以查看的页面?你必须自己滚动所有这些吗? 网络上的哪些资源可
我正在看MVC账户控制器,似乎来自于ASP.NET Webforms.有没有什么好的背景信息如何使用它?

您可以将其映射到用户数据库表,还是更好地滚动您自己的用户管理?

如何在MVC中使用它来限制登录用户可以查看的页面?你必须自己滚动所有这些吗?

网络上的哪些资源可以帮助您了解ASP.NET会员资格?

解决方法

I’m looking at the MVC account
controller…. it seems to be from
asp.net?

Scott Guthrie在他关于ASP.NET MVC Preview 4的博客条目中解释得很好.他基本上说MVC示例中的Account Controller使用ASP.NET成员资格提供者,因此可以使用其中的任何一个. (我想你可以在互联网上找到关于ASP.NET会员提供商的更多信息.)如果您不想实现/使用其中之一,修改应用程序以使用您自己的用户管理可能是最佳选择.

How do you make use of it in MVC to
restrict what pages a logged in user
can view? Do you have to roll all of
that on your own?

您可以将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");
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读