I can't seem to get a very basic cookie login example to work with MVC5 and OWIN

后端 未结 2 724
梦毁少年i
梦毁少年i 2021-01-14 09:30

I\'ve been trying to get my feet wet with ASP.net MVC 5 for 2013, but so far I\'ve failed to get even the most basic authentication working correctly.

I\'ve been rea

2条回答
  •  情书的邮戳
    2021-01-14 09:48

    I have had similar problems

    I could not figure out what was different between my app (I inherited from someone else) and the default sample code.

    I discovered that my [Authorize] attribute was not being applied by the framework even when the rest of the user management stack was working.

    I realised eventually that AuthorizeAttribute is an example of a Filter and that by adding it explicitly in the FilterConfig it started to be used as expected (even though it is not added by the default sample code):

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

    and everything as usual in the Application_Start method:

    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    

    Edit:

    Although this lets the basic Authorize attribute work, it leads to a problem where the framework isn't instantiating the attribute per-method call with the Roles property set. So I had to figure out what caused the problem. It was due to some Unity setup code:

    var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider);
    FilterProviders.Providers.Remove(oldProvider);
    

    Removing this (it was actually unused) fixed the issue, so I no longer needed FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); either

提交回复
热议问题