9
我创建了自定义成员资格提供程序,并试图创建新的“MembershipUser”时出现以下错误。无法为自定义MembershipProvider创建MembershipUser
未能从程序集 '的System.Web,版本= 4.0.0.0,文化=中性公钥= 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>
Upvote from the future。为了让我的安全组件为一个MVC项目工作得很好,而不是Windows窗体项目,您刚刚为我节省了另外4个小时的麻烦。 – Tommy 2011-03-28 07:13:54
经过10分钟的研究,这帮助我解决了一个问题。如果不是这样的话,情况会更糟糕,所以,谢谢! – jason 2013-01-10 17:16:35