获取自定义成员资格用户的 ASP.NET 自定义成员资格问题 - 没有为此对象定义无参数构造函数
创建自定义成员资格用户对象时抛出错误。详细错误信息如下。
错误:
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>
请帮我解决这个问题
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="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>
Please help me to solve this issue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建 CustomMembersipUser 实例时,您需要传递在 web.config 的成员资格部分中声明的提供程序的名称。不建议对 CustomMembershipProvider 中的值进行硬编码,而是可以使用在调用 CustomMembershipProvider.Initialize 方法时接收此值的私有字段。
例如,
您可以在 MSDN 上看到类似但更详细的示例。
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
You can see a similar but more detailed example on MSDN.