授权属性不适用于mvc 4

时间:2012-12-16 23:19:16

标签: asp.net authorization simplemembership

我使用visual studio 2012为MVC 4编写代码...我正在尝试实现基于角色的授权但似乎[授权]由于某种原因无法工作....而且我仍然可以通过网址获取页面甚至它的控制器动作也是用[Authorize]属性初始化的......第二,我甚至可以通过在全局中放入以下代码来查看整个应用程序

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new System.Web.Mvc.AuthorizeAttribute());
        filters.Add(new HandleErrorAttribute());
    }

我的路由默认设置为登录页面,因为没有未经授权的人可以访问应用

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
        );
    }

我尝试通过直接从url获取aa()视图进行测试

[Authorize]
public class HomeController : Controller
{


    public ActionResult Index()
    {
        return View();
    }

    [Authorize]
    public ActionResult aa()
    {
        return View();
    }

登录代码

[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{


    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;

        return View();
    }

    [HttpPost]

    public ActionResult Login(LoginModel model, string returnUrl)
    {

        if (ModelState.IsValid && WebSecurity.Login(model._UserName, model._Password, persistCookie: false))
        {
            return RedirectToAction("Index", "Home");                
        }

        ModelState.AddModelError("", "The user name or password provided is incorrect.");

        return View(model);
    }

和web.config

 <authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

2 个答案:

答案 0 :(得分:0)

InitializeSimpleMembership需要装饰你的HomeController,而不是AccountsController。由于您的HomeController / Index方法是整个网站的入口点,要使[Authorize]正常工作,您需要先将SimpleMembership初始化,然后再进行其他操作。

答案 1 :(得分:0)

我创建了新的MVC4网站并面临同样的问题:如何强制授权(如果未经授权,则重定向到登录页面)。

我是ASP.NET的初学者,并且找不到简单的答案(有很多关于此的内容,特别是在stackoverflow,但这些答案中没有一个帮助我)。我终于在几小时后完成了它,所以也许我会帮助这个人。

  1. 将过滤器添加到全局过滤器,这将需要在任何地方进行授权(除非您将设置AllowAnonymous,但稍后会对其进行设置)。 打开App_Start / FilterConfig.cs 并:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            // Add this line
            filters.Add(new AuthorizeAttribute());
        }
    
  2. 现在我们需要在配置文件中设置授权。 在项目的根目录中打开Web.config 并:

    <system.web>
    
        // start of modification
    
        // remove this line
        <authentication mode="None" />
    
        // add this lines
        <authentication mode="Forms">
            <forms loginUrl="~/Account/Login" timeout="30" protection="All"></forms>
        </authentication>
        <authorization>
            <deny users="?" />
        </authorization>
    
        // end of modification
    
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
    </system.web>
    
  3. 这就是全部。默认登录操作默认具有[AllowAnonymous]标记,因此它应该可以正常工作。它们不需要在控制器中手动添加它们/像:

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
            {
                ViewBag.ReturnUrl = returnUrl;
                return View();
            }
    

    希望帮助了某人。