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=&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> 

回答

21

我只注意到我在配置錯過下面的部分現在

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

作品!

+4

Upvote from the future。爲了讓我的安全組件爲一個MVC項目工作得很好,而不是Windows窗體項目,您剛剛爲我節省了另外4個小時的麻煩。 – Tommy 2011-03-28 07:13:54

+2

經過10分鐘的研究,這幫助我解決了一個問題。如果不是這樣的話,情況會更糟糕,所以,謝謝! – jason 2013-01-10 17:16:35

相關問題