【问题标题】:Simple Membership and Role Provider in MVC3/MVC4MVC3/MVC4 中的简单成员和角色提供者
【发布时间】:2014-01-20 17:01:58
【问题描述】:

我正在编写简单的成员资格和角色提供者。 我正在尝试从帖子http://www.brianlegg.com/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx 运行代码,但在运行 IIS 时显示错误:

Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Line 39:       <providers>
Line 40:         <clear />
Line 41:         <add name="LocalBankMembershipProvider" type="LocalBank.Helpers.LocalBankMembershipProvider, LocalBank" connectionStringName="LocalBankEntities" />
Line 42:       </providers>
Line 43:     </membership>

Message Parser Error: Target call threw an exception.
Line: 41

我在项目开始时犯了哪些错误? 使用 Nuget 包将项目从 mvc3 转换为 mvc4

我该如何解决这个错误?

【问题讨论】:

    标签: c# asp.net-mvc-3 asp.net-mvc-4 membership-provider roleprovider


    【解决方案1】:

    您是否遵循了同一链接上 cmets 部分中给出的建议?

    可以尝试的几件事:

    1. 可能是连接字符串错误,因此您会看到此错误。更改连接字符串(例如数据源)。如果您仍然发现问题,请调试项目并查看它到底卡在哪里以及您可以获得更多信息。我能够重现此问题,并通过更改连接字符串得到解决。

    2. 当您调试时,您会看到它会在检查角色存在时抛出异常。根据博客,您需要添加默认角色“经理”:

    唯一需要注意的是没有创建角色。如果您查看 AccountController Register HttpPost 操作的内部,您会注意到通过该站点注册的任何用户都被赋予了“会员”的默认角色。这可以更改,但无论这是什么角色,都必须首先在角色表中创建。您可以通过编写脚本(很可能是首选方法)来做到这一点,或者您可以编写一些 hacky 代码并快速调用它,因为它只需要在第一次创建角色时运行 1 次。下面是一些示例代码,您可以使用它来创建“成员”角色,修改它以满足您的需要。

    LocalBankRepository repo = new LocalBankRepository(); 
    repo.AddRole("Member"); 
    repo.Save(); 
    

    从代码中的某处调用这 3 行(或单独的 “初始化程序”项目),从那时起代码应该像 预期的。很抱歉造成混乱。

    如果您仍然遇到问题,请分享更多详细信息。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      相关资源
      最近更新 更多