将Rolemanager添加到配置文件会导致HTTP错误500.19 - 内部服务器错误

时间:2014-04-23 16:07:35

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

我为我的应用程序创建角色,我将此角色添加到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();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

但由于错误是一个自我解释的错误,删除此行

<roleManager enabled="true" />

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

  

不同情况:

     
      
  • 使用IIS 7 / 7.5时通常会发生此错误,因为在更高版本的IIS中未使用通配符(。)。*   通过删除MIME类型条目“。*”或将其替换为“
  • 来解决此问题   
  • 我们将虚拟目录的权限授予默认用户,以免出现错误。 我们将虚拟目录的权限授予默认用户,   我们应该将应用程序池标识设置为默认用户,然后将其   现在工作正常。
  •   
  • 出现此问题的原因是Application Host.config文件或Web.config文件包含格式错误的XML元素。 删除   格式不正确的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>