Cannot create MembershipUser for custom MembershipProvider
Asked Answered
S

1

10

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=&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>
Setback answered 28/10, 2010 at 18:24 Comment(0)
S
22

I just noticed I missed the below part in the configuration

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

Works now!

Setback answered 28/10, 2010 at 20:18 Comment(2)
Upvote from the future. You just saved me about another 4 hours of going bonkers on why my security assembly worked fine for an MVC project but not a Windows Forms project!Kranz
This helped me solve an issue after 10 minutes of research. It would have been a whole lot worse were it not for this, so, thanks!Achlorhydria

© 2022 - 2024 — McMap. All rights reserved.