3

我有一个 MVC 3 应用程序,当我尝试从远程服务器访问它时出现 401 错误,但是当我从本地计算机运行它时,它运行时没有任何错误。

远程服务器将 IIS 7 配置为允许表单和匿名访问并成功提供静态文件(内容文件),只有当我尝试访问 MVC 应用程序时才会遇到问题。

当我浏览到 MVC 应用程序时,我收到一条提示:“Windows 安全 - 服务器需要用户名和密码。警告:此服务器请求以不安全的方式发送您的用户名和密码(基本身份验证没有安全连接)。”

我试过输入所有我能想到的密码,但没有任何东西可以让我访问。

当我单击取消时,我收到一条服务器错误消息,显示“401 - 未经授权:由于凭据无效,访问被拒绝。您无权使用您提供的凭据查看此目录或页面。” 我已被重定向到LogOn页面

我的 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=152368 
  --> 
<configuration> 
  <configSections> 
    <section name="razorJSSettings" type="RazorJS.Configuration.RazorJSSettings, RazorJS" /> 
  </configSections> 
  <connectionStrings> 
    <!--<add name="ApplicationServices" 
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" 
         providerName="System.Data.SqlClient" />--> 
    <add name="ApplicationServices" connectionString="Data Source=SOURCE; Initial Catalog=thedb; User ID=thedbuser; Password='thedbuserpassword';" /> 
    <add name="GestmeDataContext" connectionString="metadata=res://*/Models.Gestme.csdl|res://*/Models.Gestme.ssdl|res://*/Models.Gestme.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SOURCE;initial catalog=thedb;persist security info=True;user id=thedbuser;password=thedbuserpassword;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
  </connectionStrings> 
  <appSettings> 
    <add key="webpages:Version" value="1.0.0.0" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
  </appSettings> 
  <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.Helpers, Version=1.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.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
      </assemblies> 
    </compilation> 
    <customErrors mode="Off" /> 
    <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="true"> 
      <providers> 
        <clear /> 
        <add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" /> 
      </providers> 
    </roleManager> 
    <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.Routing" /> 
        <add namespace="System.Web.WebPages" /> 
      </namespaces> 
    </pages> 
    <httpHandlers> 
      <add path="razorjs.axd" verb="GET" type="RazorJS.RazorJSHandler" /> 
    </httpHandlers> 
  </system.web> 
  <system.webServer> 
    <httpErrors errorMode="Detailed" />
    <asp scriptErrorSentToBrowser="true"/>
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules runAllManagedModulesForAllRequests="true" /> 
    <handlers> 
      <add name="RazorJSHandler" path="razorjs.axd" verb="GET" type="RazorJS.RazorJSHandler" /> 
    </handlers> 
  </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> 
      <dependentAssembly> 
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> 
        <bindingRedirect oldVersion="0.0.0.0-4.0.8.0" newVersion="4.0.8.0" /> 
      </dependentAssembly> 
    </assemblyBinding> 
  </runtime> 
  <razorJSSettings handlerPath="~/razorjs.axd"> 
    <!-- If empty all paths are valid --> 
    <allowedPaths> 
      <add path="~/Scripts" /> 
    </allowedPaths> 
  </razorJSSettings> 
</configuration> 

可能导致服务器返回 401 错误的所有问题是什么?可能是从数据库到服务器或aspnet_*表的连接?

4

3 回答 3

5

所以,真正的原因有点愚蠢。我在母版页上有一个部分视图,它的[Authorize]属性与其操作结果相关联。

以防万一其他人遇到同样的错误!

于 2012-04-25T14:02:25.397 回答
1

我相信这是因为您使用的是 AspNetWindowsTokenRoleProvider。这是试图在本地服务器上为用户获取角色,而不是从 Windows 窗体。

您应该指定 SqlRoleProvider。

于 2012-04-17T18:45:09.730 回答
1

请转到 IIS 默认网站中的“身份验证方法”并选中“集成 Windows 身份验证”。这可能是工作。

于 2012-04-18T04:52:05.843 回答