Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC5 Redirects to Login.aspx when using Windows Authentication

After upgrading from MVC 4 to MVC 5, my application (when started from within Visual Studio) gives the following error. It might also be worth noting that I am hosting both MVC5 and WebAPI2 items in the same project as there may be interference going on. I also installed the dotnetopenauth nuget package (which I have since removed):

Server Error in '/' Application.  The resource cannot be found.  Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.   Requested URL: /login.aspx 

I'm not really sure why this is as there is no authorization attributes set in the filters or on the controllers.

Global.asax:

    public class MvcApplication : HttpApplication {      protected void Application_Start() {         IDependencyInjectionBuilder dependencyInjectionBuilder = new DependencyInjectorBuilder();         var builder = new ContainerBuilder();          builder.RegisterControllers(typeof(MvcApplication).Assembly);         builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //For WebAPI dependency injection         var injector = new AutofacDependencyInjector();         dependencyInjectionBuilder.RegisterTypesAndInstances(injector, false, true);         DependencyInjectionRegistration.RegisterServices(injector);          builder.Update(injector.Container.ComponentRegistry);          var autofacDependencyResolver = new AutofacDependencyResolver(injector.Container);         DependencyResolver.SetResolver(autofacDependencyResolver);          GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(injector.Container); //For WebAPI dependency injection          AreaRegistration.RegisterAllAreas();          AutoMapperConfig.RegisterMappings(Mapper.Configuration);          //Reference: http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2         GlobalConfiguration.Configure(WebApiConfig.Register);          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);         RouteConfig.RegisterRoutes(RouteTable.Routes);         BundleConfig.RegisterBundles(BundleTable.Bundles);          GlobalConfiguration.Configuration.EnsureInitialized();  #if DEBUG         // Uncomment this line for the nhibernate profiler          // and add HibernatingRhinos.Profiler.Appender.dll         // to the PD.UserInterfacePbj project references         //HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); #endif     } } 

Root Web.config:

<?xml version="1.0" encoding="utf-8"?> <configuration>   <configSections>     <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />     </configSections>     <appSettings>         <add key="webpages:Version" value="3.0.0.0" />         <add key="webpages:Enabled" value="false" />         <add key="PreserveLoginUrl" value="true" />         <add key="ClientValidationEnabled" value="true" />         <add key="UnobtrusiveJavaScriptEnabled" value="true" />     </appSettings>     <log4net>         <appender name="NHProfAppender" type="HibernatingRhinos.Profiler.Appender.NHibernate.NHProfAppender, HibernatingRhinos.Profiler.Appender.NHibernateLog4Net">             <sink value="tcp://127.0.0.1:56107" />         </appender>         <logger name="HibernatingRhinos.Profiler.Appender.NHibernate.NHProfAppender.Setup">             <appender-ref ref="NHProfAppender" />         </logger>         <root>             <priority value="ALL" />             <appender-ref ref="ConsoleAppender" />         </root>     </log4net>     <system.web>         <customErrors mode="Off" defaultRedirect="~/Error/Index">             <error statusCode="404" redirect="~/Error/Error404" />         </customErrors>         <compilation debug="true" targetFramework="4.5" />         <httpRuntime />         <authentication mode="Windows" />         <authorization>             <deny users="?" />         </authorization>         <pages controlRenderingCompatibilityVersion="4.0">             <namespaces>                 <add namespace="System.Web.Helpers" />                 <add namespace="System.Web.Mvc" />                 <add namespace="System.Web.Mvc.Ajax" />                 <add namespace="System.Web.Mvc.Html" />                 <add namespace="System.Web.Optimization" />                 <add namespace="System.Web.Routing" />                 <add namespace="System.Web.WebPages" />                 <add namespace="My.Localization" />             </namespaces>         </pages>     </system.web>     <system.webServer>         <modules runAllManagedModulesForAllRequests="true" />         <validation validateIntegratedModeConfiguration="false" />         <handlers>             <remove name="ExtensionlessUrlHandler-Integrated-4.0" />             <remove name="OPTIONSVerbHandler" />             <remove name="TRACEVerbHandler" />             <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />         </handlers>     </system.webServer>     <runtime>         <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">             <dependentAssembly>                 <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" />                     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />                 <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />                 <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />                 <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />                 <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />                 <bindingRedirect oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />                 <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.0.0" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />                 <bindingRedirect oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />                 <bindingRedirect oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />                 <bindingRedirect oldVersion="0.0.0.0-3.3.1.4000" newVersion="3.3.1.4000" />             </dependentAssembly>             <dependentAssembly>                 <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />                 <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />             </dependentAssembly>         </assemblyBinding>     </runtime>     <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">         <bytecode-provider type="null" />         <!-- Important under Medium Trust -->         <session-factory name="Stinky Pete">             <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>             <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>             <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>             <property name="connection.connection_string"> ... </property>             <mapping assembly=" ... " />         </session-factory>     </hibernate-configuration>     <system.net>         <defaultProxy enabled="true" />         <settings>         <!--<servicePointManager checkCertificateRevocationList="true"/>-->         </settings>     </system.net>     <uri>         <!-- The uri section is necessary to turn on .NET 3.5 support for IDN (international domain names), which is necessary for OpenID urls with unicode characters in the domain/host name.      It is also required to put the Uri class into RFC 3986 escaping mode, which OpenID and OAuth require. -->         <idn enabled="All" />         <iriParsing enabled="true" />     </uri> </configuration> 

Views Web.config:

<?xml version="1.0"?> <configuration>     <configSections>         <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">             <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />             <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />         </sectionGroup>     </configSections>     <system.web.webPages.razor>         <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />         <pages pageBaseType="System.Web.Mvc.WebViewPage">             <namespaces>                 <add namespace="System.Web.Mvc" />                 <add namespace="System.Web.Mvc.Ajax" />                 <add namespace="System.Web.Mvc.Html" />                 <add namespace="System.Web.Optimization"/>                 <add namespace="System.Web.Routing" />                 <add namespace="Infragistics.Web.Mvc"/>             </namespaces>         </pages>     </system.web.webPages.razor>     <appSettings>         <add key="webpages:Enabled" value="false" />     </appSettings>     <system.web>         <httpHandlers>             <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>         </httpHandlers>         <pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.0.0.0,  Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage,  System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.0.0.0,  Culture=neutral, PublicKeyToken=31BF3856AD364E35">             <controls>                 <add assembly="System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />             </controls>         </pages>     </system.web>     <system.webServer>         <validation validateIntegratedModeConfiguration="false" />         <handlers>             <remove name="BlockViewHandler"/>             <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />         </handlers>     </system.webServer> </configuration> 
like image 819
Flea Avatar asked Nov 19 '13 17:11

Flea


People also ask

How does Windows authentication work in MVC?

When you enable Windows authentication, your web server becomes responsible for authenticating users. Typically, there are two different types of web servers that you use when creating and deploying an ASP.NET MVC application.

How can add window authentication in ASP.NET MVC?

By default MVC apps use Form Authentication and Simple Membership, so you need to make it "false" to run Windows Authentication. Select the project name in Solution Explorer and then in the Property Explorer, click to enable Windows Authentication.

What is the default authentication in MVC?

The default authentication is, Individual User Accounts.


2 Answers

I was able to resolve the issue by adding the following to the appSettings section as per the response to this question.

<add key="autoFormsAuthentication" value="false" /> <add key="enableSimpleMembership" value="false"/> 
like image 55
Flea Avatar answered Oct 05 '22 07:10

Flea


In addition, Web.Config System.web section should NOT contain this line:

<authentication mode="Forms" /> 

And in my case it worked.

like image 43
SvitlanaS Avatar answered Oct 05 '22 05:10

SvitlanaS