2013-10-26 95 views
1

我完全沮丧尝试实现自定义RoleProvider。我被挂上了“找不到默认角色提供者”。错误信息。这里是我的web.config:MVC 4.5自定义角色提供程序错误(defaultProvider无法找到)

<?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> 
    <!-- 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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </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> 
    <httpRuntime targetFramework="4.5" /> 
    <compilation debug="true" targetFramework="4.5"> 
     <assemblies> 
     <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
     </assemblies> 
    </compilation> 
    <authentication mode="Forms"> 
     <forms loginUrl="~/Account/Login" timeout="900" /> 
    </authentication> 
    <roleManager enabled="true" defaultProvider="AccountRoleProvider"> 
     <providers> 
     <clear/> 
     <add name="AccountRoleProvider" 
      type="Billing.Business.AccountRoleProvider, Billing" 
      enablePasswordRetrieval="false" enablePasswordReset="true" 
      requiresQuestionAndAnswer="false" writeExceptionsToEventLog="false" /> 
     </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" /> 
     <add namespace="System.Web.Optimization" /> 
     </namespaces> 
    </pages> 
    </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> 
</configuration> 

这里是自定义RoleProvider类的代码:

namespace Billing.Business 
{ 
    public class AccountRoleProvider : RoleProvider 
    { 
     ... 

     public override string[] GetRolesForUser(string username) 
     { 
      List<string> userRoles = new List<string>(); 
      var entities = new CRSCommonEntities(); 
      short role = entities.CrsAppUsers.First(u => u.UserName == username).UserType; 

      if (role >= (short)UserRole.ADMIN) 
      { 
       userRoles.Add(UserRole.ADMIN.ToString()); 
      } 

      if (role >= (short)UserRole.STAFF) 
      { 
       userRoles.Add(UserRole.STAFF.ToString()); 
      } 

      if (role >= (short)UserRole.CHAIN) 
      { 
       userRoles.Add(UserRole.CHAIN.ToString()); 
      } 

      if (role >= (short)UserRole.PROPERTY) 
      { 
       userRoles.Add(UserRole.PROPERTY.ToString()); 
      } 

      return userRoles.ToArray(); 
     } 

     /// <summary> 
     /// This returns true if the user has an access level at or above the request level 
     /// </summary> 
     /// <param name="username"></param> 
     /// <param name="roleName"></param> 
     /// <returns></returns> 
     public override bool IsUserInRole(string username, string roleName) 
     { 
      var entities = new CRSCommonEntities(); 
      var user = entities.AppUsers.Single(u => u.UserName == username); 

      return user.UserType >= (short)Enum.Parse(typeof(UserRole), roleName); 
     } 

     ... 
    } 
} 

如果你们能帮助我发现我在做什么错了,我只是没有看到它现在我已经开始了两天了。谢谢!

回答

1

我最后弄明白了。提供者的名称覆盖必须与web.config中设置的名称相同。我只是必须将获取返回值更改为“AccountRoleProvider”,它的工作原理!

0

type="Billing.Business.AccountRoleProvider, Billing"

是您的组件供应商位于内称为Billing?很可能你的程序集被称为更多。

如果它实际上被称为Billing,请确保包含您的Web.config的程序集参考Billing

+0

您好,非常感谢您的回复。该程序集称为Billing,web.config和提供程序在同一个应用程序中。 –

相关问题