HTTP Error 401.0 - Unauthorized IIS 8

While working on the MVC5 project, I have access to the account / login page. When I enter the wrong credentials, it tells me that the username / password is incorrect. When I enter the correct credentials, it redirects me to the home / index, so I assume that the login really works.

As soon as I go to a new page, I get the following error. HTTP Error 401.0 - Unauthorized.

Ant I'm not sure how I will continue and solve this.

My login controller

public ActionResult LogIn() { return View(); } [HttpPost] public ActionResult LogIn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (MembershipService.ValidateUser(model.UserName, model.Password)) { FormsService.SignIn(model.UserName, model.RememberMe); if (!String.IsNullOrEmpty(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return View(model); } 

And my model

 public class LogOnModel { [Required] [DisplayName("User name")] public string UserName { get; set; } [Required] [DataType(DataType.Password)] [DisplayName("Password")] public string Password { get; set; } [DisplayName("Remember me?")] public bool RememberMe { get; set; } } public interface IFormsAuthenticationService { void SignIn(string userName, bool createPersistentCookie); void SignOut(); } public class FormsAuthenticationService : IFormsAuthenticationService { public void SignIn(string userName, bool createPersistentCookie) { if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); } 

And my last web.config:

 <?xml version="1.0"?> <configuration> <connectionStrings> <add name="SaleswebEntities" connectionString= </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies> </compilation> <authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="2880" /> </authentication> <membership> <providers> <clear /> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> </providers> </membership> <profile> <providers> <clear /> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" /> </providers> </profile> <roleManager enabled="false"> <providers> <clear /> <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> </providers> </roleManager> <pages> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="System.Web.Helpers" /> <add namespace="System.Web.WebPages" /> </namespaces> </pages> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> <appSettings> <add key="ClientValidationEnabled" value="false" /> <add key="UnobtrusiveJavaScriptEnabled" value="false" /> </appSettings> </configuration> 

The connection string is not empty, but I deleted it. I do not want the publication to be published.

+7
c # asp.net-mvc asp.net-mvc-5
source share
4 answers

I had a simulated problem and there was nothing with the code, something happened to my iis and I had to reinstall it. The main thing here is to make sure that you uninstall the Windows Process Activation Service or else your ApplicationHost.config will continue to be.

+2
source

I noticed that you are using FormsService in your login controller. I think this class is specific to SharePoint. I would recommend using WebSecurity.Login() or FormsAuthentication.Authenticate() instead.

0
source

Did you notice that your Startup.cs set up the application correctly?

There should be something similar to the following:

 app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), Provider = new CookieAuthenticationProvider { // Enables the application to validate the security stamp when the user logs in. // This is a security feature which is used when you change a password or add an external login to your account. OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) } }); 
0
source

Sounds like an IIS permission issue, you should try running VS as an administrator, if you haven't already.

"HTTP Error 401.0 - Unauthorized You do not have permission to view this directory or page.

Diagnose 401.x HTTP Errors in IIS

Try to make sure that the permissions for the folders are correct. Double-click the Authentication icon in IIS. Right-click the "Anonymous Authentication" provider and select Modify. Now right-click the web application in the left pane, select "Edit Permissions ...", select the "Security" tab, click "Edit" → "Add" and add IIS APPPOOL\NameOfAppPool . Verify that the application pool identifier has read and execute permissions to this folder.

Here are some links.

Configure IIS (Windows 7) for ASP.NET/ASP.NET MVC 3

http://patrickdesjardins.com/blog/asp-net-mvc-http-error-401-0-unauthorized

https://serverfault.com/questions/348049/iis-and-http-401-0-unauthorized

0
source

All Articles