Authorize attribute not working MVC 5

2019-02-22 09:52发布

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

7条回答
劫难
2楼-- · 2019-02-22 10:05

For me, I needed to set the following appSetting

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

to

<add key="owin:AutomaticAppStartup" value="true"/>
查看更多
\"骚年 ilove
3楼-- · 2019-02-22 10:12

Do you have something like this

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

in your web.config?

查看更多
霸刀☆藐视天下
4楼-- · 2019-02-22 10:17

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楼-- · 2019-02-22 10:18

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.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-02-22 10:22

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);
}
查看更多
何必那么认真
7楼-- · 2019-02-22 10:25

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.

查看更多
登录 后发表回答