asp.net-mvc - MVC4部署到远程IIS 7.5,访问SQL Server时给出401.2错误

标签 asp.net-mvc sql-server-2008 iis

开发MVC网站以处理跟踪数据。目标是使用MVC4并将其部署到运行SQL Server 2008和IIS 7.5的Windows 2008 R2 Server。它在域中运行。

有效的原始设置是一台使用Visual Studio 2012.2的开发计算机,该计算机运行Windows 8,IIS 8(未表达)并与远程SQL Server通讯。使用Developer Server不会进行开发。我能够连接到该网站,运行任何操作,等等,而在任何日志中都没有明显的问题。

自部署到2008服务器和IIS 7.5以来,已经发生了问题。使用映射到ApplicationPoolIdentity的DefaultAppPool,我创建了一个具有绑定*:80的网站并附加了部署。设置为:

Application pool: DefaultAppPool
.Net Framework Version: 4.0
Pipeline Mode: Integrated
Pass-through authentication.


测试设置显示消息“应用程序池标识有效。”
我将虚拟目录添加到网站,并且“测试设置”显示相同的消息。
我将目录转换为应用程序,并且“测试设置”现在显示错误消息“无效的应用程序路径”。如果我使用自己的凭据,则“测试设置”将显示有效。

从远程服务器上的IIS网站->应用程序->操作->浏览*:80
我会自动收到我的主页。浏览到包含对sql服务器的调用的页面时,出现错误401.2,该错误可追溯到远程计算机上的404。

我在IIS服务器上安装了Windows身份验证,我在SQL中为IIS AppPool\DefaultAppPool创建了登录名。我在数据库中添加了与用户db_readerdb_writer相同的访问权限。我添加了身份以读取/写入和执行网站虚拟目录的权限。

我已经阅读了所有可以在此搜索中找到的内容,并且所有内容都表明问题是未安装Windows身份验证或需要运行aspnet_regiis -i ...,但是我完成了所有操作!我敢肯定这是一个配置问题,但我无法弄清楚,并在过去的两天中竭尽全力解决此问题。

Web.config部分如下:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <appSettings>
    <add key="webpages:Version" value="2.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>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Windows" />
    <authorization>
      <deny users="?" />
    </authorization>
    <pages>
      <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" />
      </namespaces>
    </pages>
    <identity impersonate="true" />
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="TestAsyncHttpHandler" />
      <add name="TestAsyncHttpHandler" path="*.svc" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /></handlers>
        <modules>
            <remove name="WebDAVModule" />
        </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.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.4.0.0" newVersion="5.4.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.4.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.4.0.0" newVersion="5.4.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <connectionStrings>
    <add name="PerfTrendDataEntities" connectionString="metadata=res://*/Models.PerfTrendDBModel.csdl|res://*/Models.PerfTrendDBModel.ssdl|res://*/Models.PerfTrendDBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=perftrenddb;initial catalog=PerfTrendData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TrendRestServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="PerformanceTrendDB.Web.SubmitResults.TrendRestService" behaviorConfiguration="TrendRestServiceBehavior">
        <endpoint name="BasicHttp" address="basic" binding="basicHttpBinding" bindingConfiguration="FileUploadConfig" contract="PerformanceTrendDB.Web.SubmitResults.ITrendRestService" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="FileUploadConfig" transferMode="StreamedRequest" />
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>


所有这些配置都可以在我的开发箱上的IIS 8上运行,我不确定迁移到IIS 7.5是否应该有很大的区别,我可以肯定地确定从IIS到SQL Server的通信一定是一个问题新机器。

编辑:添加了日志文件和跟踪文件

操作流程,打开网站,然后单击ManageUsers链接。管理用户负载,然后api调用失败。浏览器最初尝试使用匿名,然后使用凭据登录似乎发生了很多错误……因此,为了解决该问题,配置中出现了两个错误。

日志文件显示:

2013-10-16 03:05:37 10.23.34.146 GET /PerformanceData - 80 - 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 401 2 5 609
2013-10-16 03:05:39 10.23.34.146 GET /PerformanceData - 80 AMR\gjmason 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 200 0 0 2281
2013-10-16 03:07:01 10.23.34.146 GET /PerformanceData/Home/ManageUsers - 80 - 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 401 2 5 15
2013-10-16 03:07:01 10.23.34.146 GET /PerformanceData/Home/ManageUsers - 80 AMR\gjmason 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 200 0 0 484
2013-10-16 03:07:03 10.23.34.146 GET /perftrenddb/api/UserManagement - 80 - 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 401 2 5 1015
2013-10-16 03:07:03 10.23.34.146 GET /perftrenddb/api/UserManagement - 80 AMR\gjmason 10.24.158.125 Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/30.0.1599.69+Safari/537.36 404 0 2 296


压缩摘要中显示的跟踪文件:

Summary (1st error)
Url: http://perftrenddb:80/PerformanceData/Home/ManageUsers 
App Pool: DefaultAppPool 
Authentication: NOT_AVAILABLE 

Summary (2nd error - same details)
Url: http://perftrenddb:80/perftrenddb/api/UserManagement 
App Pool: ASP.NET v4.0 Classic 
Authentication: NOT_AVAILABLE 

Summary (3rd error - from a STATUS_CODE 404)
Url: http://perftrenddb:80/perftrenddb/api/UserManagement 
App Pool: ASP.NET v4.0 Classic 
Authentication: Negotiate 


那么三件事?


尝试以匿名用户身份登录,还是模拟尝试?正常?
(已修复,请参阅解决方案2)api调用将转到http://perftrenddb:80/perftrenddb/api/UserManagement,而不是我进行设置的方式,也不是它在本地计算机上运行的方式。
App Pool更改为Classic,而不是使用Integrated Pipeline的DefaultAppPool,这是问题吗?


我可以回答第二和第三条错误痕迹,因为那条在JS文件中。在开发环境中工作时是我的错(这是我第一次涉足Web开发,我通常会坚持使用应用程序库!)。对于这部分,我确实有解决方案...尽管我仍然遇到第一个问题和我不理解的应用程序池转换的错误/跟踪,但它已使我的网站正常运行。

我讨厌我不了解的怪异问题,因此,解决401.2匿名登录问题的任何见识都将是很棒的!

解决方案2
为了为我们的菜鸟修复硬编码我们的虚拟目录名称的上下文前缀,有一项解决程序消除了Developer Server / IIS Express / IIS部署问题。

将此添加到您的_Layout.cshtml或母版页:

    <script type="text/javascript">
        var config = {
            contextPath: '@Url.Content("~")'
        }
    </script>


然后在您的JS文件中添加:

var apiUri = config.contextPath + "api/MyWebApi";


其中MyWebApi是您正在访问的API控制器的名称。现在,您可以毫无问题地拨打getJSON了,即

$.getJSON(apiUri, null).success( /* Your code */ );

最佳答案

据我了解,我可以在Microsoft网站上看​​到401.2错误是因为


未尝试进行身份验证,因为服务器和客户端可以
不同意身份验证协议


我也可以猜测权限也可能有问题。

您可以使用Microsoft提供的以下工具来诊断问题

Authentication and Access Control Diagnostics 1.0

IIS Diagnostics Toolkit

希望这对面临相同问题的人有所帮助。

关于asp.net-mvc - MVC4部署到远程IIS 7.5,访问SQL Server时给出401.2错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19392855/

相关文章:

asp.net - 在 mvc3 中的 Get Call 上将数据从 View 传递到 Controller

asp.net - 使用嵌套集合更新模型,ASP.NET MVC

sql - 使用 select 语句获取子表中的记录数

c# - System.Net.Mail 不发送生产

iis - 如何在 IIS7 中配置每个文件夹和扩展的静态内容缓存?

.net - ASP.NET MVC 3 中的下拉列表

javascript - 从 HttpContext.Response 捕获 REQUIRES_AUTH 和 xhr.status 时出现问题

SQL - 按日期汇总事件(一次 5 天)

.net - 经典ASP中的自定义错误处理

c# - 尝试使用 C# 访问 Windows 7 计算机上的端口 80 时出错