Allowanonymous属性在asp.net mvc中不起作用

时间:2015-08-09 12:59:14

标签: c# asp.net-mvc-4 authentication authorization

我正在使用Web API控制器构建Web服务。但是,当我尝试使用[allowanonymous]属性修饰我的任何操作时,它不起作用...这里是一个图像

enter image description here

我只是想知道这里的错误是什么......

3 个答案:

答案 0 :(得分:3)

完全符合名称

[System.Web.Mvc.AllowAnonymous]
public ActionResult SomeAction()
{ ...

答案 1 :(得分:1)

在页面顶部,使用namespace,如:

using System.Web.Mvc;

答案 2 :(得分:1)

只需使用以下内容删除它:

   startServer("http://", "localhost", "Sample", "UI", "9090");

   public static void startServer(String protocol, String host, String webapp, String ui, String port) {
        try {
            String BASE_URI = protocol + host + ":" + port + "/" + webapp + "/";

            HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI));

            /*--- Static UI Content ---*/
            if(ui!=null && !ui.equals("")) {
                String jarPath = getJarPath(SampleGrizzlyWebServer.class);
                if (jarPath != null) {
                    CLStaticHttpHandler clStaticHttpHandler = new CLStaticHttpHandler(
                            new URLClassLoader(new URL[]{new File(jarPath).toURI().toURL()}),
                            "/", "/lib/", "/js/", "/css/");
                    ServerConfiguration sc = httpServer.getServerConfiguration();
                    sc.addHttpHandler(clStaticHttpHandler, HttpHandlerRegistration.builder().contextPath("/" + webapp + "/" + ui + "").urlPattern("").build());
                }
            }

            /*--- SampleAdmin WebappContext ---*/
            WebappContext SampleAdminContext = new WebappContext("WebappContext", "/" + webapp + "/" + "SampleAdmin");

            /*--- Servlet ---*/
            final ResourceConfig sampleAdminRc = new SampleAdminApplication();
            ServletRegistration sampleAdminRegistration = SampleAdminContext.addServlet("ServletContainer", new ServletContainer(sampleAdminRc));
            sampleAdminRegistration.addMapping("/*");

            /**
             * Deploy Server
             */
            SampleAdminContext.deploy(httpServer);
            httpServer.start();

            System.out.println("Jersey app started with WADL available at:");
            System.out.println(BASE_URI + "SampleAdmin/application.wdl");
            System.out.println("Hit enter to stop it...");
            System.in.read();
            httpServer.shutdown();
        }
        catch(Exception ex) {
           System.err.println("Error: " + ex.getMessage());
        }
    }

因为该命名空间用于WebAPI,而using System.Web.Http; 用于MVC控制器。例如,在同一文件中使用这两个名称空间可能会导致System.Web.MvcAuthorize混淆。