ASP.NET MVC账户控制器的使用指南? [英] ASP.NET MVC Account Controller usage guidelines?

查看:119
本文介绍了ASP.NET MVC账户控制器的使用指南?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待在MVC的帐户控制器,它似乎是从ASP.NET web表单。是否有关于如何使用它的任何良好的背景信息?

你能映射到用户数据库表还是更推出自己的用户管理?

你如何在MVC中使用它来限制哪些网页登录的用户可以查看?你有滚所有这一切对你自己的?

在网络上什么资源可以了解ASP.NET成员帮助?


解决方案

  

我看的MVC帐户
  控制器....它似乎是从
  asp.net?


斯科特格思里约<一个他的博客条目解释这个相当不错href=\"http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-$p$pview-4-release-part-1.aspx\">ASP.NET MVC preVIEW 4 。他基本上说,从MVC样本账户控制器使用ASP.NET成员资格提供程序,所以你可以使用任何这些。 (我认为你可以找到更多关于ASP.NET成员资格提供在互联网上。)如果你不想执行/使用其中的一个,修改应用程序使用自己的用户管理很可能是最好的选择。


  

你如何利用它来MVC
  限制登录的用户哪些页面
  可以查看?你有滚所有
  自己找?


您可以在授权属性添加到控制器类或操作方法。 (相同的<一个href=\"http://weblogs.asp.net/scottgu/archive/2008/07/14/asp-net-mvc-$p$pview-4-release-part-1.aspx\">source如上述。)

  //只有登录的用户可以访问此控制器。
[授权]
公共类SomeController:控制器
{
    #地区在这个例子中并不重要。 :]
    //也许在这里,而使用BLL的服务,而不是从DAL库,但这个例子已经是更详细的比要求。
    私人IStuffRepository stuffRepository;    公共SomeController(IStuffRepository stuffRepository)
    {
        如果(空== stuffRepository)
        {
            抛出新的ArgumentNullException(stuffRepository);
        }        this.stuffRepository = stuffRepository;
    }
    #endregion    //在授权属性是继承的 - 只有在用户可以使用索引操作记录。
    公众的ActionResult指数()
    {
        返回查看();
    }    //版主可以标记的东西。
    [授权(角色=版主)]
    公众的ActionResult标志(INT ID)
    {
        this.stuffRepository.Flag(ID);
        返回RedirectToAction(「指数」);
    }    //管理员答SysOps可以删除的东西。
    [授权(角色=管理员,系统操作员)]
    公众的ActionResult删除(INT ID)
    {
        this.stuffRepository.Delete(ID);
        返回RedirectToAction(「指数」);
    }    //只有约叶可以改变物体的东西。 ;)
    //(这可能是废话,当然,但我不能让任何更好的例子了。我责备事实上,它是在深夜。:))
    [授权(用户=公司\\\\约叶)]
    公众的ActionResult ChangeId(INT OLDID,INT NEWID)
    {
        this.stuffRepository.ChangeId(OLDID,NEWID);
        返回RedirectToAction(「指数」);
    }
}

I'm looking at the MVC account controller, and it seems to be from ASP.NET webforms. Is there any good background information on how to use it?

Can you map it to a user database table or is it better to roll your own user management?

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?

What resources on the web can help with understanding the ASP.NET Membership?

解决方案

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

Scott Guthrie explains this quite well in his blog entry about ASP.NET MVC Preview 4. He basically says that the Account Controller from the MVC sample uses the ASP.NET membership provider, so you can use any of those. (I think you can find out more about ASP.NET membership providers on the internet.) If you do not want to implement/use one of those, modifying the application to use your own user management would probably be the best option.

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?

You can add the Authorize attribute to the controller class or action method. (Same source as above.)

// 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");
    }
}

这篇关于ASP.NET MVC账户控制器的使用指南?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆