从 MembershipUser 继承 - Asp.net/C#
我有一个继承自 MembershipUser 的客户类。
当我加载 Customer 对象时,我希望能够轻松设置基类中的所有值。
我尝试声明隐藏只读 MembershipUser 属性(例如 ProviderUserKey)的 Customer 属性,然后设置这些属性,但是当我需要设置它们时,基类属性为 null。
这是我的类 Customer.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Security;
using _FusioLibrary.Utilities;
namespace _CutMyCostsLibrary.BusinessObjects
{
public class Customer : MembershipUser
{
#region Properties
private Boolean active;
public Boolean Active
{
get { return active; }
set { active = value; }
}
private String address1;
public String Address1
{
get { return address1; }
set { address1 = value; }
}
private String address2;
public String Address2
{
get { return address2; }
set { address2 = value; }
}
private String companyName;
public String CompanyName
{
get { return companyName; }
set { companyName = value; }
}
private String county;
public String County
{
get { return county; }
set { county = value; }
}
private DateTime createDate;
public DateTime CreateDate
{
get { return createDate; }
set { createDate = value; }
}
private Int32 customerId;
public Int32 CustomerId
{
get { return customerId; }
set { customerId = value; }
}
private Boolean deleted;
public Boolean Deleted
{
get { return deleted; }
set { deleted = value; }
}
private String firstName;
public String FirstName
{
get { return firstName; }
set { firstName = value; }
}
private String lastName;
public String LastName
{
get { return lastName; }
set { lastName = value; }
}
private String mobile;
public String Mobile
{
get { return mobile; }
set { mobile = value; }
}
private DateTime modifyDate;
public DateTime ModifyDate
{
get { return modifyDate; }
set { modifyDate = value; }
}
private String phone;
public String Phone
{
get { return phone; }
set { phone = value; }
}
private DateTime reminderEmailSentDate;
public DateTime ReminderEmailSentDate
{
get { return reminderEmailSentDate; }
set { reminderEmailSentDate = value; }
}
private String town;
public String Town
{
get { return town; }
set { town = value; }
}
private String userIdAspNet;
public String UserIdAspNet
{
get { return userIdAspNet; }
set { userIdAspNet = value; }
}
#endregion
#region MembershipUser Properties Hiding
read only properties need to be writeable
private DateTime creationDate;
public DateTime CreationDate
{
get { return creationDate; }
set { creationDate = value; }
}
private bool isLockedOut;
public bool IsLockedOut
{
get { return isLockedOut; }
set { isLockedOut = value; }
}
private bool isOnline;
public bool IsOnline
{
get { return isOnline; }
set { isOnline = value; }
}
private DateTime lastLockoutDate;
public DateTime LastLockoutDate
{
get { return lastLockoutDate; }
set { lastLockoutDate = value; }
}
private DateTime lastPasswordChangedDate;
public DateTime LastPasswordChangedDate
{
get { return lastPasswordChangedDate; }
set { lastPasswordChangedDate = value; }
}
private string passwordQuestion;
public string PasswordQuestion
{
get { return passwordQuestion; }
set { passwordQuestion = value; }
}
private string providerName;
public string ProviderName
{
get { return providerName; }
set { providerName = value; }
}
private object providerUserKey;
public object ProviderUserKey
{
get { return providerUserKey; }
set { providerUserKey = value; }
}
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
#endregion
#region Load
public static Customer Load(IDataReader iDataReader)
{
Customer oCustomer = new Customer();
//populate the customer properties
oCustomer.Active = DataReaderUtility.GetBool(iDataReader["Active"]);
oCustomer.Address1 = DataReaderUtility.GetString(iDataReader["Address1"]);
oCustomer.Address2 = DataReaderUtility.GetString(iDataReader["Address2"]);
oCustomer.CompanyName = DataReaderUtility.GetString(iDataReader["CompanyName"]);
oCustomer.County = DataReaderUtility.GetString(iDataReader["County"]);
oCustomer.CreateDate = DataReaderUtility.GetDateTime(iDataReader["CreateDate"]);
oCustomer.CustomerId = DataReaderUtility.GetInt(iDataReader["CustomerId"]);
oCustomer.Deleted = DataReaderUtility.GetBool(iDataReader["Deleted"]);
oCustomer.FirstName = DataReaderUtility.GetString(iDataReader["FirstName"]);
oCustomer.LastName = DataReaderUtility.GetString(iDataReader["LastName"]);
oCustomer.Mobile = DataReaderUtility.GetString(iDataReader["Mobile"]);
oCustomer.ModifyDate = DataReaderUtility.GetDateTime(iDataReader["ModifyDate"]);
oCustomer.Phone = DataReaderUtility.GetString(iDataReader["Phone"]);
oCustomer.ReminderEmailSentDate = DataReaderUtility.GetDateTime(iDataReader["ReminderEmailSentDate"]);
oCustomer.Town = DataReaderUtility.GetString(iDataReader["Town"]);
oCustomer.UserIdAspNet = DataReaderUtility.GetString(iDataReader["UserIdAspNet"]);
//populate the MembershipUser properties
if(!string.IsNullOrEmpty(oCustomer.UserIdAspNet))
{
MembershipUser oMembershipUser = Membership.GetUser(Guid.Parse(oCustomer.UserIdAspNet));
oCustomer.Comment = oMembershipUser.Comment;
oCustomer.CreationDate = oMembershipUser.CreationDate;
oCustomer.Email = oMembershipUser.Email;
oCustomer.IsApproved = oMembershipUser.IsApproved;
oCustomer.IsLockedOut = oMembershipUser.IsLockedOut;
oCustomer.IsOnline = oMembershipUser.IsOnline;
oCustomer.LastActivityDate = oMembershipUser.LastActivityDate;
oCustomer.LastLockoutDate = oMembershipUser.LastLockoutDate;
oCustomer.LastLoginDate = oMembershipUser.LastLoginDate;
oCustomer.LastPasswordChangedDate = oMembershipUser.LastPasswordChangedDate;
oCustomer.PasswordQuestion = oMembershipUser.PasswordQuestion;
oCustomer.ProviderName = oMembershipUser.ProviderName;
oCustomer.ProviderUserKey = oMembershipUser.ProviderUserKey;
oCustomer.UserName = oMembershipUser.UserName;
}
return oCustomer;
}
#endregion
}
}
现在,当我尝试执行以下代码时,我收到错误,因为基类 ProviderUserKey 为 null。
Customer oCustomer = CustomerManager.GetCustomer(UrlUtility.GetStringValue("UserId"));
//first we need to update the username
CustomerManager.UpdateCustomerUserName(oCustomer.UserIdAspNet, this.txtEmail.Text);
//Insert the modified data into the object
oCustomer.Active = this.cbActive.Checked;
oCustomer.Address1 = this.txtAddress1.Text;
oCustomer.Address2 = this.txtAddress2.Text;
oCustomer.CompanyName = this.txtCompanyName.Text;
oCustomer.County = this.txtCounty.Text;
oCustomer.FirstName = this.txtFirstName.Text;
oCustomer.LastName = this.txtLastName.Text;
oCustomer.Mobile = this.txtMobile.Text;
oCustomer.ModifyDate = DateTime.Now;
oCustomer.Phone = this.txtPhone.Text;
oCustomer.Town = this.txtTown.Text;
oCustomer.Email = this.txtEmail.Text;
//Update The Customer table
CustomerManager.UpdateCustomer(oCustomer);
//update the membership tables
Membership.UpdateUser(oCustomer);
最后一行 Membership.UpdateUser(oCustomer) 出错,因为基类中的 ProviderUserKey 为 null。
任何想法如何填充基类,记住 ProviderUserKey 是只读的?
代码示例(我的代码已修改)将非常有帮助并值得赞赏。
I have a customer class that inherits from MembershipUser.
When i load a Customer object i want to be able to set all the values in the base class easily.
I try to declare properties of Customer that hide readonly MembershipUser properties such as ProviderUserKey and then set those properties, however the base class properties are null when i need them to be set.
here is my class Customer.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Security;
using _FusioLibrary.Utilities;
namespace _CutMyCostsLibrary.BusinessObjects
{
public class Customer : MembershipUser
{
#region Properties
private Boolean active;
public Boolean Active
{
get { return active; }
set { active = value; }
}
private String address1;
public String Address1
{
get { return address1; }
set { address1 = value; }
}
private String address2;
public String Address2
{
get { return address2; }
set { address2 = value; }
}
private String companyName;
public String CompanyName
{
get { return companyName; }
set { companyName = value; }
}
private String county;
public String County
{
get { return county; }
set { county = value; }
}
private DateTime createDate;
public DateTime CreateDate
{
get { return createDate; }
set { createDate = value; }
}
private Int32 customerId;
public Int32 CustomerId
{
get { return customerId; }
set { customerId = value; }
}
private Boolean deleted;
public Boolean Deleted
{
get { return deleted; }
set { deleted = value; }
}
private String firstName;
public String FirstName
{
get { return firstName; }
set { firstName = value; }
}
private String lastName;
public String LastName
{
get { return lastName; }
set { lastName = value; }
}
private String mobile;
public String Mobile
{
get { return mobile; }
set { mobile = value; }
}
private DateTime modifyDate;
public DateTime ModifyDate
{
get { return modifyDate; }
set { modifyDate = value; }
}
private String phone;
public String Phone
{
get { return phone; }
set { phone = value; }
}
private DateTime reminderEmailSentDate;
public DateTime ReminderEmailSentDate
{
get { return reminderEmailSentDate; }
set { reminderEmailSentDate = value; }
}
private String town;
public String Town
{
get { return town; }
set { town = value; }
}
private String userIdAspNet;
public String UserIdAspNet
{
get { return userIdAspNet; }
set { userIdAspNet = value; }
}
#endregion
#region MembershipUser Properties Hiding
read only properties need to be writeable
private DateTime creationDate;
public DateTime CreationDate
{
get { return creationDate; }
set { creationDate = value; }
}
private bool isLockedOut;
public bool IsLockedOut
{
get { return isLockedOut; }
set { isLockedOut = value; }
}
private bool isOnline;
public bool IsOnline
{
get { return isOnline; }
set { isOnline = value; }
}
private DateTime lastLockoutDate;
public DateTime LastLockoutDate
{
get { return lastLockoutDate; }
set { lastLockoutDate = value; }
}
private DateTime lastPasswordChangedDate;
public DateTime LastPasswordChangedDate
{
get { return lastPasswordChangedDate; }
set { lastPasswordChangedDate = value; }
}
private string passwordQuestion;
public string PasswordQuestion
{
get { return passwordQuestion; }
set { passwordQuestion = value; }
}
private string providerName;
public string ProviderName
{
get { return providerName; }
set { providerName = value; }
}
private object providerUserKey;
public object ProviderUserKey
{
get { return providerUserKey; }
set { providerUserKey = value; }
}
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
#endregion
#region Load
public static Customer Load(IDataReader iDataReader)
{
Customer oCustomer = new Customer();
//populate the customer properties
oCustomer.Active = DataReaderUtility.GetBool(iDataReader["Active"]);
oCustomer.Address1 = DataReaderUtility.GetString(iDataReader["Address1"]);
oCustomer.Address2 = DataReaderUtility.GetString(iDataReader["Address2"]);
oCustomer.CompanyName = DataReaderUtility.GetString(iDataReader["CompanyName"]);
oCustomer.County = DataReaderUtility.GetString(iDataReader["County"]);
oCustomer.CreateDate = DataReaderUtility.GetDateTime(iDataReader["CreateDate"]);
oCustomer.CustomerId = DataReaderUtility.GetInt(iDataReader["CustomerId"]);
oCustomer.Deleted = DataReaderUtility.GetBool(iDataReader["Deleted"]);
oCustomer.FirstName = DataReaderUtility.GetString(iDataReader["FirstName"]);
oCustomer.LastName = DataReaderUtility.GetString(iDataReader["LastName"]);
oCustomer.Mobile = DataReaderUtility.GetString(iDataReader["Mobile"]);
oCustomer.ModifyDate = DataReaderUtility.GetDateTime(iDataReader["ModifyDate"]);
oCustomer.Phone = DataReaderUtility.GetString(iDataReader["Phone"]);
oCustomer.ReminderEmailSentDate = DataReaderUtility.GetDateTime(iDataReader["ReminderEmailSentDate"]);
oCustomer.Town = DataReaderUtility.GetString(iDataReader["Town"]);
oCustomer.UserIdAspNet = DataReaderUtility.GetString(iDataReader["UserIdAspNet"]);
//populate the MembershipUser properties
if(!string.IsNullOrEmpty(oCustomer.UserIdAspNet))
{
MembershipUser oMembershipUser = Membership.GetUser(Guid.Parse(oCustomer.UserIdAspNet));
oCustomer.Comment = oMembershipUser.Comment;
oCustomer.CreationDate = oMembershipUser.CreationDate;
oCustomer.Email = oMembershipUser.Email;
oCustomer.IsApproved = oMembershipUser.IsApproved;
oCustomer.IsLockedOut = oMembershipUser.IsLockedOut;
oCustomer.IsOnline = oMembershipUser.IsOnline;
oCustomer.LastActivityDate = oMembershipUser.LastActivityDate;
oCustomer.LastLockoutDate = oMembershipUser.LastLockoutDate;
oCustomer.LastLoginDate = oMembershipUser.LastLoginDate;
oCustomer.LastPasswordChangedDate = oMembershipUser.LastPasswordChangedDate;
oCustomer.PasswordQuestion = oMembershipUser.PasswordQuestion;
oCustomer.ProviderName = oMembershipUser.ProviderName;
oCustomer.ProviderUserKey = oMembershipUser.ProviderUserKey;
oCustomer.UserName = oMembershipUser.UserName;
}
return oCustomer;
}
#endregion
}
}
Now when i try to execute the following code i get an error because the base class ProviderUserKey is null.
Customer oCustomer = CustomerManager.GetCustomer(UrlUtility.GetStringValue("UserId"));
//first we need to update the username
CustomerManager.UpdateCustomerUserName(oCustomer.UserIdAspNet, this.txtEmail.Text);
//Insert the modified data into the object
oCustomer.Active = this.cbActive.Checked;
oCustomer.Address1 = this.txtAddress1.Text;
oCustomer.Address2 = this.txtAddress2.Text;
oCustomer.CompanyName = this.txtCompanyName.Text;
oCustomer.County = this.txtCounty.Text;
oCustomer.FirstName = this.txtFirstName.Text;
oCustomer.LastName = this.txtLastName.Text;
oCustomer.Mobile = this.txtMobile.Text;
oCustomer.ModifyDate = DateTime.Now;
oCustomer.Phone = this.txtPhone.Text;
oCustomer.Town = this.txtTown.Text;
oCustomer.Email = this.txtEmail.Text;
//Update The Customer table
CustomerManager.UpdateCustomer(oCustomer);
//update the membership tables
Membership.UpdateUser(oCustomer);
it errors on the last line Membership.UpdateUser(oCustomer) because ProviderUserKey in the base class is null.
Any Ideas how to populate the base class, bearing in mind ProviderUserKey is readonly?
Code samples (my code modified) would be really helpful and appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。
Readonly
表示您无法设置它,只能读取它的值。获取可更新的正确填充的
MembershipUser
的唯一方法是通过Membership.GetUser
,它将返回默认MembershipUser
的实例> 类,而不是自定义类的实例。要执行您想要执行的操作,您需要创建一个自定义MembershipProvider
,它返回Customer
而不是MemberhipUser
,但这就是可能带来的麻烦多于其价值。我发现效果更好的是让Customer
“包装”MembershipUser
而不是扩展它。我已经用两种方法完成了(创建自定义MembershipProvider
并围绕MembershipUser
创建包装器),并且使用包装器绝对是更简单的方法:然后而不是隐藏只读属性,实际上用它们做一些事情。将 Customer.IsLockedOut 从
true
更改为false
意味着需要发生一些事情,因此在您的Customer.IsLockedOut
属性中,而不是仅仅存储一个值没有任何意义,请调用MembershipUser.UnlockUser()
:如果您不希望在设置属性时运行它,那么您需要跟踪值是否更改,并调用
解锁用户()
从您的CustomerManager.UpdateCustomer
方法。You can't.
Readonly
means you can't set it, you can only read its value.The only way to get a properly populated
MembershipUser
that you can update is throughMembership.GetUser
, which is going to return an instance of the defaultMembershipUser
class, not an instance of your custom class. To do what you're trying to do, you would need to create a customMembershipProvider
that returns aCustomer
instead of aMemberhipUser
, but that's probably more trouble than it's worth. What I've found works much better is to makeCustomer
"wrap"MembershipUser
instead of extending it. I've done it both ways (creating a customMembershipProvider
and creating a wrapper aroundMembershipUser
), and using a wrapper is definitely the easier approach:Then instead of hiding the readonly properties, actually do something with them. Changing Customer.IsLockedOut from
true
tofalse
means something needs to happen, so in yourCustomer.IsLockedOut
property, instead of just storing a value that doesn't mean anything, callMembershipUser.UnlockUser()
:If you don't want that to run when you set the property, then you would need to track whether the value changed, and call
UnlockUser()
from yourCustomerManager.UpdateCustomer
method.