自定义成员资格用户对象创建时抛出错误。详细信息错误消息如下。
错误:
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
我的自定义MembershipUser类代码如下所示。
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;
}
}
当我尝试使用上面的自定义用户类创建对象时出现错误(对象创建代码如下)
CustomMembershipUser membershipUser = new CustomMembershipUser(this.Name,
username,
Guid.Parse(userID.ToString()),
email,
passwordQuestion,
comment,
isApproved,
isLockedOut,
creationDate,
lastLoginDate,
lastActivityDate,
lastPasswordChangedDate,
lastLockedOutDate, "ss");
Web配置配置如下。
<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="Data Source=GET2CODE\GET2CODE;Initial Catalog=CatalogueViewer;User Id=sa;Password=hammer;MultipleActiveResultSets=True"" 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>
请帮我解决这个问题
答案 0 :(得分:2)
当您创建CustomMembersipUser的实例时,需要传递在web.config的membership身份部分中声明的提供者的名称。不建议对CustomMembershipProvider中的值进行硬编码,而是可以使用在调用CustomMembershipProvider.Initialize方法时接收此值的私有字段。
例如
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");
...
}
}
您可以在MSDN上看到类似但更详细的示例。