Global Filter on MVC 5 not working

Asked

Viewed 187 times

2

Hello. I am following the tutorial below with the intention of better understanding the authentication process of . net Identity with OWIN.

http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1

When accessing Home, it should redirect to auth/login and it’s not happening. It’s like you’re ignoring Authorizeattribute().

Follow my codes.

Startup.Cs

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Security.Cookies;

[assembly: OwinStartup(typeof(DotNetIdentidade.Startup))]

namespace DotNetIdentidade
    {
        public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "ApplicationCookie",
                LoginPath = new PathString("/auth/login")
            });
        }
    }
}

Filterconfig.Cs

using System.Web.Mvc;
namespace DotNetIdentidade
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new AuthorizeAttribute());
        }
    }
}

Global.asax.Cs

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

namespace DotNetIdentidade
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
        }
    }
}

Grateful.

  • Where is the code of HomeController?

  • And oh Gypsy. I am without the pc now. But the code of the Homecontroller is basic, I did not make any changes when I created. From what I noticed in the tutorial, the idea is not to authorize the access of any controller unless those I put as [Allowanonymous].

  • True. The tutorial suggests this. I need to test also because I use another approach: mark with [Authorize] where I want authentication.

  • This morning I turned on my PC, I didn’t change anything in the project and now when I went to run it worked as described in the tutorial. Is it related to the PC reboot? Any cache in which Globalfilter was not updated? Before posting my doubt I had already given a Clean on Solution, a rebuild and nothing.

  • On second thought, if I changed Global.asax I have to restart the application so IIS can see my changes. Does it make sense? If so, then surely this was the cause. I had already run the application once and IIS Express used the same instance already started with old Global.asax.

  • It could be a lot of things. Too bad I can’t answer you with something useful :/ Global.asax is one of them.

Show 1 more comment

1 answer

2

After restarting the pc the code worked as expected. I believe it has something to do with the reboot of IIS Express as there have been changes in Global.asax after the first run.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.