我正在构建一个自定义MembershipProvider,更准确地说是GetUser函数。
因此我有一个自定义MembershipUser。
public class CustomMemberShipUser : MembershipUser
{
public CustomMemberShipUser (
string providerName,
string email,
object providerUserKey,
string name,
string passwordQuestion,
string comment,
bool isApproved,
bool isLockedOut,
DateTime creationDate,
DateTime lastLoginDate,
DateTime lastActivityDate,
DateTime lastPasswordChangedDate,
DateTime lastLockoutDate
): base(
providerName, email, providerUserKey, name, passwordQuestion,
comment, isApproved, isLockedOut, creationDate, lastLoginDate,
lastActivityDate, lastPasswordChangedDate, lastLockoutDate)
{
}
}
在MembershipProvider的GetUser函数中,我获取用户数据并将它们放入CustomMemberShipUser。
public override MembershipUser GetUser(string email, bool userIsOnline)
{
User u = _db.Users.Where(x => x.Email == email).First();
CustomMemberShipUser customUser = new CustomMemberShipUser (
"CustomMemberShipUser ",
u.Email,
u.id,
u.Email,
"",
"",
true,
false,
u.CreateDate,
DateTime.MinValue,
DateTime.MinValue,
DateTime.MinValue,
DateTime.MinValue);
return customUser ;
}
如您所见,我使用电子邮件作为MemberShip的名称,我不需要大多数其他参数。
有没有办法让呼叫变得更简单?我不想使用空字符串和最小日期值初始化MembershipUser。
提前致谢
答案 0 :(得分:4)
您是否可以调整CustomMembershipUser为您执行“填充”
public class CustomMemberShipUser : MembershipUser
{
public CustomMemberShipUser (
string email,
object providerUserKey,
): base(
"CustomMemberShipUser", email, providerUserKey, email, String.Empty,
String.Empty, true, false, DateTime.MinValue, DateTime.MinValue,
DateTime.MinValue, DateTime.MinValue, DateTime.MinValue)
{
}
}
它不能解决问题,但会整理您的提供商,这将成为
public override MembershipUser GetUser(string email, bool userIsOnline)
{
User u = _db.Users.Where(x => x.Email == email).First();
CustomMemberShipUser customUser = new CustomMemberShipUser (u.Email, u.id);
return customUser ;
}
我认为您的CustomMembershipUser正在公开您未向我们展示的一些其他属性。就目前而言,您可以返回MembershipUser。有了上述唯一的好处,您的CustomMembershipUser为您提供了CustomMembershipProvider中更清洁的构造