仅具有所需参数的自定义 MembershipUser

发布于 2024-11-14 13:26:32 字数 2001 浏览 2 评论 0原文

我正在构建一个自定义 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 ;
    }

正如您所看到的,我使用电子邮件作为会员资格的名称,并且不需要大多数其他参数。

有没有办法让通话更简单?我不想用空字符串和最小日期值初始化 MembershipUser。

提前致谢

I am building a custom MembershipProvider more precisely the GetUser function.

Therefor i have a custom 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)
    {
    }
}

In the GetUser function of the MembershipProvider i get the user data and put them into the 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 ;
    }

As you can see i use the email as name for the MemberShip and i don't need most of the other parameters.

Is there a way to make the call simpler? I don't want to initalize the MembershipUser with empty Strings and minimal Date values.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜兔 2024-11-21 13:26:32

您可以调整您的 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 中的结构更清晰

Could you adapt your CustomMembershipUser to do the 'padding' for you

   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)
        {
        }
    }

It doesn't solve the problem but it will tidy up your provider which will become

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 ;
    }

I presume your CustomMembershipUser is exposing some additional properties that you are not showing us. As it stands you could just return a MembershipUser. With the above the only benefit your CustomMembershipUser gives you is the cleaner construction in your CustomMembershipProvider

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文