Authorize attribute not working MVC 5

2019-02-22 09:36发布

问题:

I have separated the Models into Specific project

After that MVC default [Authorize] attribute not working in my application

When i try to login the application it is not login the application nor it is redirecting to the specific page

回答1:

Do you have something like this

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

in your web.config?



回答2:

Check if you have removed the forms authorization module. Some of the new templates remove forms authorization by default. If it has been removed, comment it out. It will look like this in your web.config:

<remove name="FormsAuthentication" />

I found this question looking for a very similar error, so I thought it would be good to post my solution.



回答3:

For me, I needed to set the following appSetting

<add key="owin:AutomaticAppStartup" value="false"/>

to

<add key="owin:AutomaticAppStartup" value="true"/>


回答4:

I had the same issue with Authorize not working. At first I thought it was the code. But everything was right - it built with no errors. I then restarted my dev machine and it started working. It worked the first time, then had the same issue with it not working. Think it must be storing the value from the first cycle and not clearing it. (only a theory)



回答5:

For me It has a very trivial mistake. I was authenticated yet. Owin Authentication remains the user logged in despite the browsers close. I need to express logoff with following code:

public void IdentitySignout() {
   AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie,
   DefaultAuthenticationTypes.ExternalCookie);
}


回答6:

For me, I've just deleted the cookies from Google Chrome and it worked.



回答7:

I just ran into this issue today, and wanted to add my solution for anyone who might also run into this error. Nothing else worked for me. I had recently done an installer for one of the other projects in this solution, and I must have unchecked the "Build" box in Configuration Manager.

With that unchecked, it didn't re-build when I added the [Authorize] tag, even though I did make cshtml page changes that were showing up. I didn't figure it out until I put a breakpoint in my controller action and noticed that I never hit it. I even tried returning null from the controller, and the application still navigated to the new page.

So, long story short, check to make sure you are Building that project in Configuration Manager, that's what worked for me.