我创建了一个自定义成员资格提供程序,并且尝试创建一个新的“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="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>
解决方法
我刚刚注意到我错过了配置中的下面部分
type="MyTestApp.Membership.TestMembershipProvider,MyTestApp.Membership"
现在工作!