1

我想我可能只是在这里遗漏了一些小东西,但我不确定它在哪里。我正在尝试实现一个自定义成员资格提供程序,以便我可以将 Membership API 用于 asp.net mvc 应用程序。我正在创建的自定义成员资格提供程序适用于存储在 web.config 文件的凭据部分中的用户。我收到的错误消息是:

无法加载文件或程序集“CompanyName.App_Code.Membership”或其依赖项之一。该系统找不到指定的文件。

我的代码如下所示:

网络配置

<membership defaultProvider="WebConfigMembershipProvider">
    <providers>
        <clear />
        <add name="WebConfigMembershipProvider"
            applicationName="CompanyName"
            Description="Web Configuration Membership Provider"
            type="CompanyName.App_Code.Membership.WebConfigMembershipProvider, CompanyName.App_Code.Membership" />
    </providers>
</membership> 

目录结构

App_Code
  -Membership
    -WebConfigMembershipProvider.cs : MembershipProvider

WebConfigMembershipProvider.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.Configuration;
    using System.Configuration;
    using System.Web.Helpers;

    namespace CompanyName.App_Code.Membership
    {
        public class WebConfigMembershipProvider : MembershipProvider
        {
            public override bool ValidateUser(string username, string password)
            {
                Configuration webConfiguration = WebConfigurationManager.OpenWebConfiguration(path: "~");
                AuthenticationSection authenticationSection = (AuthenticationSection)webConfiguration.GetSection("system.web/authentication");
                FormsAuthenticationUser storedUser = authenticationSection.Forms.Credentials.Users[username];
                return storedUser != null && Crypto.VerifyHashedPassword(storedUser.Password, password);
            }
/*The rest of MembershipProvider's methods implemented here throwing NotImplementedException*/

当我在登录控制器中调用 Membership.ValidateUser 时发生错误。

4

1 回答 1

3

看起来type可能配置不正确。在我的项目中,我有这样的东西。

type="CompanyName.App_Code.Membership.WebConfigMembershipProvider, CompanyName" 

this is assuming you also have CompanyName as the rootnamespace in your .csproj file.

<RootNamespace>CompanyName</RootNamespace>

On a side note, CompanyName.Membership.WebConfigMemberShipProvider itself looks good. App_Code in namespace looks little bit odd to me. It is just my preference.

You can also try to clean, build again and then try.

于 2013-10-11T18:48:15.003 回答