gpt4 book ai didi

c# - 使用自己的属性扩展 .NET MembershipUser

转载 作者:太空狗 更新时间:2023-10-29 17:42:31 25 4
gpt4 key购买 nike

我编写了一个自定义的 MembershipProvider,除了一件小事,它工作得非常好,我想稍微扩展一下 MembershipUser 类。

默认是这样的:

MembershipUser member = new MembershipUser(
providerName: Membership.Provider.Name,
name: db.userName,
providerUserKey: null,
email: db.userEmail,
passwordQuestion: "",
comment: "",
isApproved: true,
isLockedOut: false,
creationDate: db.creationDate,
lastLoginDate: db.lastLoginDate,
lastActivityDate: db.lastActivityDate,
lastPasswordChangedDate: DateTime.Now,
lastLockoutDate: DateTime.Now
);

但我想扩展一下,像这样:

MembershipUser member = new MembershipUser(
providerName: Membership.Provider.Name,
name: db.userName,
guid: db.userGuid,
company: db.companyName,
companyGuid: db.companyGuid,
whatever: db.whatever,
providerUserKey: null,
email: db.userEmail,
passwordQuestion: "",
comment: "",
isApproved: true,
isLockedOut: false,
creationDate: db.creationDate,
lastLoginDate: db.lastLoginDate,
lastActivityDate: db.lastActivityDate,
lastPasswordChangedDate: DateTime.Now,
lastLockoutDate: DateTime.Now
);

有没有办法扩展默认类?

最佳答案

当然可以!

只需创建一个扩展 MembershipUser 的类:

public class CustomUser : MembershipUser
{
// your custom properties/methods go here
}

在您的 CustomMembershipProvider 中,您可以返回您的 CustomUser 对象。您只需在客户端应用程序中强制转换为 CustomUser。像这样:

var myUserObject = Membership.GetUser() as CustomUser;

关于c# - 使用自己的属性扩展 .NET MembershipUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5833703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com