无法为自定义 MembershipProvider 创建 MembershipUser

发布于 2024-09-29 09:52:23 字数 2989 浏览 4 评论 0原文

我创建了一个自定义会员资格提供程序,并在尝试创建新的“MembershipUser”时收到以下错误。

无法从程序集“System.Web,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“MyTestApp.Membership.TestMembershipProvider”。

我正在从单元测试项目中运行它,所以我不确定这是否导致了问题,但我确实包含了 System.Web、System.Web.ApplicationServices 以及对 MyApp.Membership 和 MyApp.DataModels (实体对象)的引用。

错误发生在我的“GetUser”函​​数内,如下所示,我的配置也如下。

public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
    try
    {
        AccountEntities db = new AccountEntities();

        if ((providerUserKey is Guid) == false)
        {
            return null;
        }

        User user = (from u in db.Users
                     where u.UserId == (Guid)providerUserKey
                     && u.Application.LoweredApplicationName == this.ApplicationName.ToLower()
                     select u).FirstOrDefault();

        if (user != null)
        {   // ERROR: Starts here, user object is correct, data is all there.
            return new MembershipUser(this.ProviderName, user.UserName, (object)user.UserId, user.Email, user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate, user.LastLoginDate, user.LastActivityDate, user.LastPasswordChangedDate, user.LastLockoutDate);
        }
        else
            return null;
    }
    catch (Exception ex)
    {
        this.WriteToEventLog(ex, "Unable to get user from object '{" + ((Guid)providerUserKey).ToString() + "}'.", "Get User");
        return null;
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="AccountEntities" connectionString="metadata=res://*/Account.AccountDataModel.csdl|res://*/Account.AccountDataModel.ssdl|res://*/Account.AccountDataModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source=&quotEDITED&quot;;Initial Catalog=CustomAuthentication;Persist Security Info=True;User ID=EDITED;Password=EDITED;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <membership defaultProvider="TestMembershipProvider" userIsOnlineTimeWindow="15">
      <providers>
        <clear/>
        <add  name="TestMembershipProvider" type="MyTestApp.Membership.TestMembershipProvider"
              applicationName="/"
              description="Membership Test"
              enablePasswordReset="true"
              enablePasswordRetrieval="true"
              maxInvalidPasswordAttempts="3"
              minRequiredNonAlphanumericCharacters="8"
              minRequiredPasswordLength="8"
              passwordAttemptWindow="30"
              requiresQuestionAndAnswer="true"
              requiresUniqueEmail="true" />
      </providers>
    </membership>
  </system.web>
</configuration>

I created a custom membership provider and am getting the following error trying to create a new "MembershipUser".

Could not load type 'MyTestApp.Membership.TestMembershipProvider' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

I am running this from a Unit Test project, so I'm not sure if that's causing the issue, but I did include System.Web, System.Web.ApplicationServices as well as a reference to MyApp.Membership and MyApp.DataModels (Entity objects).

The error happens inside my "GetUser" function which is below, my configuration is also below.

public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
    try
    {
        AccountEntities db = new AccountEntities();

        if ((providerUserKey is Guid) == false)
        {
            return null;
        }

        User user = (from u in db.Users
                     where u.UserId == (Guid)providerUserKey
                     && u.Application.LoweredApplicationName == this.ApplicationName.ToLower()
                     select u).FirstOrDefault();

        if (user != null)
        {   // ERROR: Starts here, user object is correct, data is all there.
            return new MembershipUser(this.ProviderName, user.UserName, (object)user.UserId, user.Email, user.PasswordQuestion, user.Comment, user.IsApproved, user.IsLockedOut, user.CreateDate, user.LastLoginDate, user.LastActivityDate, user.LastPasswordChangedDate, user.LastLockoutDate);
        }
        else
            return null;
    }
    catch (Exception ex)
    {
        this.WriteToEventLog(ex, "Unable to get user from object '{" + ((Guid)providerUserKey).ToString() + "}'.", "Get User");
        return null;
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="AccountEntities" connectionString="metadata=res://*/Account.AccountDataModel.csdl|res://*/Account.AccountDataModel.ssdl|res://*/Account.AccountDataModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source="EDITED";Initial Catalog=CustomAuthentication;Persist Security Info=True;User ID=EDITED;Password=EDITED;MultipleActiveResultSets=True'" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <membership defaultProvider="TestMembershipProvider" userIsOnlineTimeWindow="15">
      <providers>
        <clear/>
        <add  name="TestMembershipProvider" type="MyTestApp.Membership.TestMembershipProvider"
              applicationName="/"
              description="Membership Test"
              enablePasswordReset="true"
              enablePasswordRetrieval="true"
              maxInvalidPasswordAttempts="3"
              minRequiredNonAlphanumericCharacters="8"
              minRequiredPasswordLength="8"
              passwordAttemptWindow="30"
              requiresQuestionAndAnswer="true"
              requiresUniqueEmail="true" />
      </providers>
    </membership>
  </system.web>
</configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-10-06 09:52:23

我刚刚注意到我错过了配置中的以下部分,

type="MyTestApp.Membership.TestMembershipProvider, MyTestApp.Membership"

现在可以工作了!

I just noticed I missed the below part in the configuration

type="MyTestApp.Membership.TestMembershipProvider, MyTestApp.Membership"

Works now!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文