开发者

ASP.NET Custom membership issue with get custom membership user -No parameterless constructor defined for this object

Error throws when custom membership user object create. Details error message is as below.

Error:

System.Configuration.ConfigurationErrorsException was caught
  Message=No parameterless constructor defined for this object. (D:\Projects\Sample\SampleMembership\web.config line 23)
  Source=System.Web
  BareMessage=No parameterless constructor defined for this object.
  Filename=D:\Projects\Sample\SampleMembership\web.config
  Line=23
  StackTrace:
       at System.Web.Configuration.ProvidersHelper.InstantiateProvider(ProviderSettings providerSettings, Type providerType)
       at System.Web.Configuration.ProvidersHelper.InstantiateProviders(ProviderSettingsCollection configProviders, ProviderCollection providers, Type providerType)
       at System.Web.Security.Membership.InitializeSettings(Boolean initializeGeneralSettings, RuntimeConfig appConfig, MembershipSection settings)
       at System.Web.Security.Membership.Initialize()
       at System.Web.Security.MembershipAdapter.get_Providers()
       at System.Web.Security.MembershipUser..ctor(String providerName, String name, Object providerUserKey, String email, String passwordQuestion, String comment, Boolean isApproved, Boolean isLockedOut, DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate, DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
       at Asys.CatalogueViewer.Membership.CustomMembershipUser..ctor(String providername, String username, Object providerUserKey, String email, String passwordQuestion, String comment, Boolean isApproved, Boolean 

My custom membershipUser class code is as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using  System.Web.Security;
public class CustomMembershipUser : MembershipUser
{    
    private string _webSite;
    public string WebSite
    {
        get { return _webSite; }
        set { _webSite = value; }
    }
       public CustomMembershipUser()
           {
           }
    public CustomMembershipUser(string providername,
                              string username,
                              object providerUserKey,
                              string email,
                              string passwordQuestion,
                              string comment,
                              bool isApproved,
                              bool isLockedOut,
                              DateTime creationDate,
                              DateTime lastLoginDate,
                              DateTime lastActivityDate,
                              DateTime lastPasswordChangedDate,
                              DateTime lastLockedOutDate,
                              string webSite) :
        base(providername,
                                   username,
                                   providerUserKey,
                                   email,
                                   passwordQuestion,
                                   comment,
                                   isApproved,
                                   isLockedOut,
                                   creationDate,
                                   lastLoginDate,
                                   lastActivityDate,
                                   lastPasswordChangedDate,
                                   lastLockedOutDate)
    {
        this.WebSite =webSite;
    }
}

Error throws when I try to create object using above custom user class(object creation code is as below)

CustomMembershipUser membershipUser = new CustomMembershipUser(this.Name,
              username,
             Guid.Parse(userID.ToString()),
              email,
         开发者_运维知识库     passwordQuestion,
              comment,
              isApproved,
              isLockedOut,
              creationDate,
              lastLoginDate,
              lastActivityDate,
              lastPasswordChangedDate,
              lastLockedOutDate, "ss");

Web config configuration for this is as below.

<configuration>
  <connectionStrings>
    <add name="ModelContainer" connectionString="metadata=res://*/Data.Model.csdl|res://*/Data.Model.ssdl|res://*/Data.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=GET2CODE\GET2CODE;Initial Catalog=CatalogueViewer;User Id=sa;Password=hammer;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
    <system.web>
        <machineKey validationKey= "23729FFE7E16B1DD03D3BC8B45280254DA09CE2ABF5C1AC2FFD5DA04AB2521DBD7E6A8EFBE49C63DA2DA55708545A4562C75E8495415CB7B0456720652A9F160"
decryptionKey= "D643C94989E6D3A76E265AA4A565108FA727DBC7AB87EDE85F578DC78A8E407C" validation="SHA1" decryption="AES"/>

        <compilation debug="true" targetFramework="4.0">
            <assemblies>
                <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
        <authentication mode="Forms" >
            <forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
        </authentication>
        <membership defaultProvider="CustomMembershipProvider" userIsOnlineTimeWindow="15">
            <providers  >
                <clear/>                
                <add name="CustomMembershipProvider" type="ASys.Membership.CustomMembershipProvider"  enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" passwordFormat="Encrypted"  connectionStringName="ModelContainer"   />              
            </providers>            
        </membership>

        <profile>
            <providers>
                <clear/>
                <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
            </providers>
        </profile>
        <roleManager enabled="false">
            <providers>
                <clear/>
                <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/>
                <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/>
            </providers>
        </roleManager>
    </system.web>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

Please help me to solve this issue


You need to pass the name of the provider as it is declared in the membership section of the web.config when you create an instance of CustomMembersipUser. It is not recommanded to hardcode the value in the CustomMembershipProvider, instead you could use a private field that receives this value when the CustomMembershipProvider.Initialize method is called.

For example

public class CustomMembershipProvider: MembershipProvider
{
    private providerName = null;
    ...

    public override void Initialize(string name, NameValueCollection config)
    {
        providerName = name; // The friendly name of the provider
        ...
    }

    public override MembershipUser TestCreateUser(string username, bool userIsOnline)
    {
        CustomMembershipUser membershipUser = new CustomMembershipUser(providerName,
              username,
              Guid.Parse(userID.ToString()),
              email,
              passwordQuestion,
              comment,
              isApproved,
              isLockedOut,
              creationDate,
              lastLoginDate,
              lastActivityDate,
              lastPasswordChangedDate,
              lastLockedOutDate, "ss");
        ...
    }
}

You can see a similar but more detailed example on MSDN.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜