Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 授权不工作_Asp.net Mvc_Asp.net Mvc 3_Forms Authentication_Authorize Attribute - Fatal编程技术网

Asp.net mvc 授权不工作

Asp.net mvc 授权不工作,asp.net-mvc,asp.net-mvc-3,forms-authentication,authorize-attribute,Asp.net Mvc,Asp.net Mvc 3,Forms Authentication,Authorize Attribute,嗨,在用尽了在互联网上找到的几乎所有教程之后,我仍然无法解决我的问题,看起来尽管我的控制器上有授权标签,但它仍然允许每个请求,即使它们没有登录 网络配置 <authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="2880" /> </authentication> 登录 寄存器全局过滤器 public static void RegisterGlobalFil

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

网络配置

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

寄存器全局过滤器

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

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

我错过什么了吗

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


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

检查会话cookie是否已创建:

还请尝试返回用于登录的用户名。使用下面的代码,或者只是使用调试器查看此值

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

检查Global.asax.cs是否缺少以下类型:

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

嗨,我有这个菜单栏在我的页面顶部,我所做的是,我已经点击了主页链接,然后它会启动主页控制器。。令我惊讶的是,我的get metho即使没有logging@anonymous1110这很奇怪,因为若您尝试从头开始创建一个新的应用程序,那个么它就会工作(它会将您重定向到登录页面)。我的建议是创建一个新的mvc项目-比较web配置,查看global.asax.My user identity.name使用我的windows nt登录。这种情况不应该发生,对吗?因为它应该使用表单身份验证。我怎样才能解决这个问题?不像你用的那样。因此,当您调用此测试操作时,您得到了用户名,这意味着您已登录。现在尝试使用FormsAuthentication.SignOut()注销;然后再次尝试调用测试操作。当我尝试登录时,它现在显示我的表单登录用户名。我注意到,当我没有通过登录页面登录时,我的user.identity.name似乎是我的windows nt登录名。这是否正确?即使我使用FormsAuthentication.SignOut()注销,也是如此;我的用户名也是我的windows登录名@_@我想我这里的问题是,当我没有登录时,我的user.identity.name使用我的windows nt登录,如果我通过登录页面登录,我的identity.name将更改为我的表单登录。奇怪的
[Authorize]
public ActionResult Test()
{
    return Content(HttpContext.Current.User.Identity.Name);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;