HTTP错误401.0 - 未经授权的IIS 8

时间:2015-06-25 07:29:09

标签: c# asp.net-mvc asp.net-mvc-5

在MVC5项目上工作,我可以访问帐户/登录页面。当我输入错误的凭据时,它告诉我用户名/密码不正确。当我输入正确的凭据时,它会将我重定向到home / index,因此我认为登录确实有效。

在访问新页面时,我收到以下错误。 HTTP错误401.0 - 未经授权。

Ant我不确定如何继续解决这个问题。

我的登录控制器

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);
}

我的模特

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);
    }

最后我的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>

连接字符串不为空,但我确实删除了它,我不希望它公开发布。

4 个答案:

答案 0 :(得分:2)

我有一个类似的问题,代码没什么,我的iis确实发生了一些事情,我不得不重新安装它。这里的关键是确保您卸载Windows进程激活服务,否则您的ApplicationHost.config仍将存在。

答案 1 :(得分:0)

我注意到您在登录控制器中使用FormsService。我认为这个类是特定于SharePoint的。我建议改为使用WebSecurity.Login()FormsAuthentication.Authenticate()

答案 2 :(得分:0)

您是否检查过您的Startup.cs是否正确配置了应用程序?

在那里应该有类似的东西:

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))
                }
            });

答案 3 :(得分:0)

听起来像IIS权限问题,如果您还没有,请尝试以管理员身份运行VS.

“HTTP错误401.0 - 未经授权 您无权查看此目录或页面。“

Diagnose 401.x HTTP errors on IIS

尝试确保文件夹的权限正确。 双击IIS中的身份验证功能。右键单击"Anonymous Authentication"提供程序,然后选择编辑。现在,右键单击左窗格中的Web应用程序,选择“编辑权限...”,选择“安全”选项卡,然后单击“编辑” - >添加并添加IIS APPPOOL\NameOfAppPool。确保应用程序池标识具有该文件夹的读取和执行权限。

以下是一些链接。

Configuring 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