【问题标题】:add Rolemanager to config file causes HTTP Error 500.19 - Internal Server Error将 Rolemanager 添加到配置文件会导致 HTTP 错误 500.19 - 内部服务器错误
【发布时间】:2014-06-08 14:38:47
【问题描述】:

我为我的应用程序创建了一个角色,并且我将此角色添加到了 web.config,如您所见:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="authentication" connectionString="Data Source=.;Initial Catalog=EducationDB;User ID=sa; password=1" providerName="System.Data.SqlClient" />
    <add name="EducationDBEntities" connectionString="metadata=res://*/EducationModel.csdl|res://*/EducationModel.ssdl|res://*/EducationModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;Initial Catalog=EducationDB;User ID=sa; password=1;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <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>
    <roleManager enabled="true" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <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>
    <!--<membership defaultProvider="AspNetSqlMembershipProvider">
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="authentication" />
      </providers>
    </membership>-->

    <roleManager defaultProvider="CustomRoleProvider" enabled="true" cacheRolesInCookie="false">
      <providers>
        <clear />
        <add name="CustomRoleProvider" type="EducationMVC.Infrastructure.MyRoleProvider"  />
      </providers>
    </roleManager>

  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <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>
  </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="0.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>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>
</configuration>

如你所见,这部分代码是我的角色:

<roleManager defaultProvider="CustomRoleProvider" enabled="true" cacheRolesInCookie="false">
      <providers>
        <clear />
        <add name="CustomRoleProvider" type="EducationMVC.Infrastructure.MyRoleProvider"  />
      </providers>
    </roleManager>

所以运行后我得到这个错误:

配置部分“system.web/roleManager”已定义。部分必须 每个配置文件只出现一次。请参阅帮助主题 异常 HTTP 错误 500.19 - 请求的内部服务器错误 页面无法访问,因为相关的配置数据 页面无效

而这部分 web.config 是亮点:

   47:     <roleManager defaultProvider="CustomRoleProvider" enabled="true" cacheRolesInCookie="false">

这是什么原因造成的?

这是我定义的角色:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using EducationModel;

namespace EducationMVC.Infrastructure
{

    public class MyRoleProvider:RoleProvider
    {
       private readonly EducationDBEntities _dbcontext = new EducationDBEntities();

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }


        public override string[] GetRolesForUser(string username)
        {

            var objUser = _dbcontext.Users.FirstOrDefault(x => x.AppUserName == username);
            if (objUser == null)
            {
                return null;
            }
            else
            {
                string[] ret = objUser.Roles.Select(x => x.RoleName).ToArray();
                return ret;
            }

        }


        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}

【问题讨论】:

  • 你在问什么问题?
  • 我问我收到了错误!!!HTTP 错误 500.19 - 内部服务器错误
  • @JohnSaunders 我编辑了我的问题错误部分,它说我定义了角色管理器两次
  • 你检查过 machine.config 吗?您可能已经在其中定义了一个 roleManager。
  • 不,我没有检查。!!我该怎么做?

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 config


【解决方案1】:

但由于错误是不言自明的,请删除此行

<roleManager enabled="true" />

【讨论】:

  • 我删除了这个错误 :: 在应用程序的预启动初始化阶段无法调用此方法。
【解决方案2】:

我只是将这些行添加到我的 web.config

<add key="enableSimpleMembership" value="false"/>
<add key="autoFormsAuthentication" value="false"/>

【讨论】:

    【解决方案3】:

    不同情况:

    • 此错误通常在使用 IIS 7/7.5 时发生,因为在更高版本的 IIS 中未使用通配符 (.)。* 通过删除 MIME 类型条目“.*”或将其替换为“ 来解决此问题
    • 我们将虚拟目录的权限授予默认用户,因此我们得到了错误。 我们将权限授予默认用户和 我们应该将应用程序池标识设置为默认用户,然后将其 现在工作正常。
    • 出现此问题的原因是 Application Host.config 文件或 Web.config 文件包含格式错误的 XML 元素。 删除 ApplicationHost.config 文件或 Web.config 文件。 如果会话时间状态创建问题,则将此代码添加到 web.config 文件中

    如果发生 IIS 配置错误...如何解决此问题

    • 在运行时输入 inetmgr 以打开 IIS。
    • 从“站点”面板中选择您的网站。
    • 对于该站点,单击右侧操作面板中的编辑权限链接。
    • 现在将打开一个新的属性窗口。
    • 从属性窗口中选择安全选项卡。
    • 从组或用户名中选择用户。
    • 现在点击所选用户的编辑按钮。
    • 如果选中任何列出的拒绝复选框,则将其删除。
    • 根据您所需的权限级别。

    enter image description here

    <system.web>
         <authentication mode="Forms">
               <forms loginUrl="~/Page.aspx" timeout="2000"/>
         </authentication>
    </system.web>
    

    【讨论】:

    • 如果您解释一下这段代码 sn-p 如何解决原始问题,您的回答会更有用。
    猜你喜欢
    • 2023-04-06
    • 2012-06-15
    • 2012-06-03
    • 1970-01-01
    • 2016-08-21
    • 2020-09-08
    • 2015-11-12
    • 2015-09-05
    相关资源
    最近更新 更多