MVC 5 Redirecting to the login page Does not work with OWIN

I am trying to use my OWIN. I created two MVC 5 projects. One with authentication using Aspnet.Identity, and the other as an empty project.

I added the following to the emptyp project:

  • Account Controller with Login Action and Eligible

  • Startup.cs and another partial Startup.cs with

public partial class Startup { public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "ApplicationCookie", LoginPath = new PathString("/Account/Login") }); } } 

I decorated the "Home" action with the [Authorize] attribute in the "About" browser.

When I start the first project and go to the "About" screen, before entering it, it is redirected to the login action, as expected. When I do the same for the second project, I get "HTTP Error 401.0 - Unauthorized" instead of redirecting.

Any idea what might make the second behave this way?

+6
source share
2 answers

Ive created two new similar projects and was able to reproduce your mistake.

In an empty project, I had to install Microsoft.Owin.Host.SystemWeb (via Nuget), and as soon as I did this, I got a bunch of errors in my Startup.cs class. It ended:

 [assembly: OwinStartupAttribute(typeof(v2.Startup))] namespace v2 { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); } public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "ApplicationCookie", LoginPath = new PathString("/Account/Login") }); } } } 

In the end, I can now see / see the appearance of my input when I call the About () method, decorated with the [Authorize] attribute.

Hope this helps! Vince

+5
source

Per ASP.NET MVC 5 Web.config: "FormsAuthenticationModule" or "FormsAuthentication"

 <system.webServer> <modules> <remove name="FormsAuthentication" /> </modules> </system.webServer> 

for added security, I left the "typo" handler (in case Microsoft changes it later by providing me)

 <system.webServer> <modules> <remove name="FormsAuthenticationModule" /> <remove name="FormsAuthentication" /> </modules> </system.webServer> 
+6
source

Source: https://habr.com/ru/post/956906/


All Articles