【问题标题】:Authorize Not working授权不工作
【发布时间】:2014-09-11 07:27:32
【问题描述】:

嗨,在用尽互联网上找到的几乎所有教程后,我仍然无法解决我的问题,似乎尽管我的控制器上有授权标签,但即使他们没有登录,它仍然允许每个请求。

网络配置

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

家庭控制器

 [Authorize]
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }

登录

  public ActionResult Login()
    {
        InitializeDropdown();
        return View();
    }

    [HttpPost]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        //selects model state errors if any
        var errors = ModelState.Values.SelectMany(v => v.Errors);
        if (ModelState.IsValid)
        {
            //AccountService accountService = new AccountService();

            bool loginsuccess = AccountService.login(model.UserName, model.Password, model.Domain);

            if (loginsuccess == false)
            {

                ModelState.AddModelError("", Session["Error"].ToString());
            }
            else
            {
                FormsAuthentication.SetAuthCookie(model.UserName, false);
                if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
                //FormsAuthentication.RedirectFromLoginPage(model.UserName, false);
            }
        }
        return View(model);
    }

注册全局过滤器

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

全球阿萨克斯

   public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        //WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        //AuthConfig.RegisterAuth();


    }


    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

我错过了什么吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 forms-authentication authorize-attribute


    【解决方案1】:

    不,您的代码似乎正确。甚至主控制器上的 Authorize 属性也是多余的,因为您已经在 RegisterGlobalFilters 方法中为所有控制器全局注册了它。

    AccountService.login 方法有效吗?也许它总是返回 true。

    【讨论】:

    • 嗨,我的页面顶部有这个菜单栏,我正在做的是我已经点击了主页链接,然后它会触发主页控制器..令我惊讶的是,我的 get 方法甚至会推动不记录
    • @anonymous1110 这很奇怪,因为如果您尝试从头开始创建一个新应用程序,那么它将起作用(它将您重定向到登录页面)。我的建议是创建一个新的 mvc 项目 - 比较网络配置,查看 global.asax。
    【解决方案2】:

    检查会话cookie是否创建:

    还可以尝试返回您用于登录的用户名。使用下面的代码或仅使用调试器查看此值。

    [Authorize]
    public ActionResult Test()
    {
        return Content(HttpContext.Current.User.Identity.Name);
    }
    

    【讨论】:

    • 我的用户 identity.name 使用我的 windows nt 登录。这不应该是这样吧?因为它应该使用表单身份验证。我该如何解决这个问题?
    • 不是您使用 的情况。因此,当您调用此测试操作时,您将获得您的用户名,这意味着您已登录。现在尝试使用 FormsAuthentication.SignOut(); 注销。并尝试再次调用测试操作。
    • 当我尝试登录时,它现在显示我的表单登录用户名。我注意到当我没有通过登录页面登录时,我的 user.identity.name 似乎是我的 windows nt 登录名。这是正确的吗?
    • 即使我使用 FormsAuthentication.SignOut();我的用户身份名称也是我的 Windows 登录名。@_@
    • 我想我的问题是当我没有登录我的 user.identity.name 使用我的 windows nt 登录时,如果我通过登录页面登录,我的 identity.name 更改为我的表单登录.奇怪
    【解决方案3】:

    检查 Global.asax.cs 是否缺少这些类型:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-09
      • 2015-08-08
      • 2019-08-07
      • 1970-01-01
      • 2015-12-18
      • 2018-04-04
      • 1970-01-01
      • 2019-06-29
      相关资源
      最近更新 更多