尝试创建“超级用户类”使用 ASPNET ProfileBase 和 MembershipUser

发布于 2024-08-22 11:23:14 字数 7896 浏览 6 评论 0 原文

我最近安装了aspnetdb并一直在定制 出于我的应用程序设计目的。我已经扩展了ASPNET 提供者作为我的课程设计的一部分:MyMembershipProvider、MyProfileProvider、 和 MyRoleProvider。那里一切都运转良好。

现在,根据我的系统的要求,我需要添加自定义数据,但我不需要 想使用aspnet_Profile中提供的名称/值设计。所以,我有 创建了两个自定义表:

dbo.Profile

CREATE TABLE [dbo].[Profiles](
    [profileid] [int] IDENTITY(1,1) NOT NULL,
    [userid] [uniqueidentifier] NOT NULL,
    [username] [varchar](255) COLLATE Latin1_General_CI_AI NOT NULL,
    [applicationname] [varchar](255) COLLATE Latin1_General_CI_AI NOT NULL,
    [confirmcode] [varchar](255) COLLATE Latin1_General_CI_AI NOT NULL,
    [isanonymous] [bit] NULL,
    [lastactivity] [datetime] NULL,
    [lastupdated] [datetime] NULL,
 CONSTRAINT [PK__Profiles__1DB06A4F] PRIMARY KEY CLUSTERED 
(
    [profileid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [PKProfiles] UNIQUE NONCLUSTERED 
(
    [username] ASC,
    [applicationname] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

dbo.ProfileData

CREATE TABLE [dbo].[ProfileData](
    [profiledataid] [int] IDENTITY(1,1) NOT NULL,
    [profileid] [int] NOT NULL,
    [lastname] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [firstname] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [alternateemail] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [zipcode] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [birthmonth] [tinyint] NULL,
    [birthday] [tinyint] NULL,
    [birthyear] [int] NULL,
    [gender] [varchar](10) COLLATE Latin1_General_CI_AI NULL,
    [city] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [state] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [country] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [ipaddress] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [sessionid] [bigint] NULL,
 CONSTRAINT [PK_ProfileData] PRIMARY KEY CLUSTERED 
(
    [profiledataid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

现在,由于我有扩展 ASPNET 基础结构的自定义数据,所以我有 编写了一个自定义用户类,有助于扩展该数据作为配置文件的一部分 快照。我基本上遵循了 John Galloway 在 此博客

public class CustomUser : ProfileBase
{

        public customuser() {}

         public static CustomUser GetCustomUserProfile(string username)
        {
            return System.Web.Profile.ProfileBase.Create(username) as CustomUser;
        }

        #region ProfileBase Extended Properties

        [SettingsAllowAnonymous(false), CustomProviderData("FirstName;string")]
        public string FirstName { get { return base["FirstName"] as string; } set { base["FirstName"] = value; } }

        [SettingsAllowAnonymous(false), CustomProviderData("LastName;string")]
        public string LastName { get { return base["LastName"] as string; } set { base["LastName"] = value; } }

        [SettingsAllowAnonymous(false), CustomProviderData("AlternateEmail;string")]
        public string AlternateEmail { get { return base["AlternateEmail"] as string; } set { base["AlternateEmail"] = value; } }

       // AND SO ON...

        #endregion

}

好的,到目前为止一切顺利。我有一个 CustomUser 类,可用于获取和设置 配置文件数据如下:

CustomUser _cu = CustomUser.GetUserProfile(username);

除了继承自的属性之外,这将返回我的所有自定义字段 配置文件库。伟大的。

但是,如果我想将其他属性添加到用户的个人资料中怎么办?即,那些提供 通过 ASPNET 的 MembershipUser 类,如 IsOnline、PasswordQuestion、IsLockedOut 等... 要从 MembershipUser 获取基本属性,我可以尝试类似的操作:

public class CustomUser : MembershipUser
    {
        public CustomUser(string providerName, string name, object providerUserKey, string email,
                                string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
                                DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
                                DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
            : base(
                providerName, name, providerUserKey, email, passwordQuestion, comment, isApproved, isLockedOut,
                creationDate, lastLoginDate, lastActivityDate, lastPasswordChangedDate, lastLockoutDate)
        {
        }

        protected CustomUser()
        {
        }

        // e.g. no desire to use Profile, can just add data
        // say, from a flat record containing all user data
        public string MyCustomField { get; set; }
    }

但是,因为我也从 ProfileBase 继承,而 C# 没有 允许多重继承(即,我不能执行 CustomUser: ProfileBase, MembershipUser), 我遇到了一些问题。构建用户类的最佳方法是什么 所以 User _user = new User();返回与经过身份验证的用户相关的全部属性?

下面是我第一次尝试这个“超级用户”课程,但我真的很难 弄清楚如何创建类对象的新实例,以便 CustomUser 和 MembershipUser 已创建。

我以正确的方式处理这件事吗?

    public class User
    {

        public User() { }

        public static User GetUserProfile(string username)
        {
             // return combined profile based on username
        }

        #region CustomUser Members

        private CustomUser _customUser
        {
            get
            {       
                if (UserName != null)
                {
                    try
                    {
                        return ProfileBase.Create(UserName) as CustomUser;
                    }
                    catch { return null;  }
                }
                else
                {
                    try 
                    {
                        // this will work if the site user is log'd in
                        return ProfileBase.Create(Membership.GetUser().UserName) as CustomUser;
                    }
                    catch { return null;  }
                }

            }
        }

        public string FirstName
        {
            get
            {
                if (_customUser != null)
                {
                    return _customUser.FirstName;
                }
                return string.Empty;
            }

            set
            {
                if (_customUser != null)
                {
                    _customUser.FirstName = value;
                }
            }
        }

        public string LastName
        {
            get
            {
                if (_customUser != null)
                {
                    return _customUser.LastName;
                }
                return string.Empty;
            }

            set
            {
                if (_customUser != null)
                {
                    _customUser.LastName = value;
                }
            }
        }


        #endregion

        #region MembershipUser Members

        //corresponding MembershipUser
        private MembershipUser _membershipUser
        {
            get
            {
                if (UserName != null)
                {
                    return Membership.GetUser(UserName);
                }
                return null;
            }
        }

        //Properties looked up from MembershipUser

        public string UserName
        {
            get
            {
                if (_membershipUser != null)
                {
                    return _membershipUser.UserName;
                }
                return string.Empty;
            }

        }

        public string Email
        {
            get
            {
                if (_membershipUser != null)
                {
                    return _membershipUser.Email;
                }
                return string.Empty;
            }

        }

        public object ProviderUserKey
        {
            get
            {
                if (_membershipUser != null)
                {
                    return _membershipUser.ProviderUserKey;
                }
                return null;
            }
        }


        #endregion

     }

}

I have recently installed the aspnetdb and have been customizing
it for the purposes of my application design. I have extended the ASPNET
providers as part of my class design: MyMembershipProvider, MyProfileProvider,
and MyRoleProvider. Everything works well there.

Now, per the requierments of my system, I need to add custom data and I don't
want to use the name / value design provided in aspnet_Profile. So, I have
created two custom tables:

dbo.Profile

CREATE TABLE [dbo].[Profiles](
    [profileid] [int] IDENTITY(1,1) NOT NULL,
    [userid] [uniqueidentifier] NOT NULL,
    [username] [varchar](255) COLLATE Latin1_General_CI_AI NOT NULL,
    [applicationname] [varchar](255) COLLATE Latin1_General_CI_AI NOT NULL,
    [confirmcode] [varchar](255) COLLATE Latin1_General_CI_AI NOT NULL,
    [isanonymous] [bit] NULL,
    [lastactivity] [datetime] NULL,
    [lastupdated] [datetime] NULL,
 CONSTRAINT [PK__Profiles__1DB06A4F] PRIMARY KEY CLUSTERED 
(
    [profileid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [PKProfiles] UNIQUE NONCLUSTERED 
(
    [username] ASC,
    [applicationname] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

and

dbo.ProfileData

CREATE TABLE [dbo].[ProfileData](
    [profiledataid] [int] IDENTITY(1,1) NOT NULL,
    [profileid] [int] NOT NULL,
    [lastname] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [firstname] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [alternateemail] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [zipcode] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [birthmonth] [tinyint] NULL,
    [birthday] [tinyint] NULL,
    [birthyear] [int] NULL,
    [gender] [varchar](10) COLLATE Latin1_General_CI_AI NULL,
    [city] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [state] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [country] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [ipaddress] [varchar](50) COLLATE Latin1_General_CI_AI NULL,
    [sessionid] [bigint] NULL,
 CONSTRAINT [PK_ProfileData] PRIMARY KEY CLUSTERED 
(
    [profiledataid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Now, since I have custom data extending the ASPNET infrastructure, I have
written a custom user class that helps extend that data as part of the profile
snapshot. I basically followed John Galloway's suggestions in this blog

public class CustomUser : ProfileBase
{

        public customuser() {}

         public static CustomUser GetCustomUserProfile(string username)
        {
            return System.Web.Profile.ProfileBase.Create(username) as CustomUser;
        }

        #region ProfileBase Extended Properties

        [SettingsAllowAnonymous(false), CustomProviderData("FirstName;string")]
        public string FirstName { get { return base["FirstName"] as string; } set { base["FirstName"] = value; } }

        [SettingsAllowAnonymous(false), CustomProviderData("LastName;string")]
        public string LastName { get { return base["LastName"] as string; } set { base["LastName"] = value; } }

        [SettingsAllowAnonymous(false), CustomProviderData("AlternateEmail;string")]
        public string AlternateEmail { get { return base["AlternateEmail"] as string; } set { base["AlternateEmail"] = value; } }

       // AND SO ON...

        #endregion

}

OK, so far so good. I have a CustomUser class that I can use for getting and setting
profile data as such:

CustomUser _cu = CustomUser.GetUserProfile(username);

This will return all my custom fields in addition to the properties inherited from
ProfileBase. Great.

But what if I want to add other properties to the User's profile? Namely, those provided
by ASPNET's MembershipUser class such as IsOnline, PasswordQuestion, IsLockedOut, etc...
To get base properties from MembershipUser, I could try something like:

public class CustomUser : MembershipUser
    {
        public CustomUser(string providerName, string name, object providerUserKey, string email,
                                string passwordQuestion, string comment, bool isApproved, bool isLockedOut,
                                DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate,
                                DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
            : base(
                providerName, name, providerUserKey, email, passwordQuestion, comment, isApproved, isLockedOut,
                creationDate, lastLoginDate, lastActivityDate, lastPasswordChangedDate, lastLockoutDate)
        {
        }

        protected CustomUser()
        {
        }

        // e.g. no desire to use Profile, can just add data
        // say, from a flat record containing all user data
        public string MyCustomField { get; set; }
    }

But, because I have also been inheriting from ProfileBase and C# does not
allow multiple inheritance (ie., I can't do CustomUser: ProfileBase, MembershipUser),
I am stuck with a bit of a problem. What is the best way to architect my user class
so User _user = new User(); returns the full breadth of properties relevant to a authenticated user?

Below is my first stab at this "SuperUser" class but I am having a real hard time
figuring out how to create a new instance of the class object such that both CustomUser
and MembershipUser are created.

Am I going about this the right way?

    public class User
    {

        public User() { }

        public static User GetUserProfile(string username)
        {
             // return combined profile based on username
        }

        #region CustomUser Members

        private CustomUser _customUser
        {
            get
            {       
                if (UserName != null)
                {
                    try
                    {
                        return ProfileBase.Create(UserName) as CustomUser;
                    }
                    catch { return null;  }
                }
                else
                {
                    try 
                    {
                        // this will work if the site user is log'd in
                        return ProfileBase.Create(Membership.GetUser().UserName) as CustomUser;
                    }
                    catch { return null;  }
                }

            }
        }

        public string FirstName
        {
            get
            {
                if (_customUser != null)
                {
                    return _customUser.FirstName;
                }
                return string.Empty;
            }

            set
            {
                if (_customUser != null)
                {
                    _customUser.FirstName = value;
                }
            }
        }

        public string LastName
        {
            get
            {
                if (_customUser != null)
                {
                    return _customUser.LastName;
                }
                return string.Empty;
            }

            set
            {
                if (_customUser != null)
                {
                    _customUser.LastName = value;
                }
            }
        }


        #endregion

        #region MembershipUser Members

        //corresponding MembershipUser
        private MembershipUser _membershipUser
        {
            get
            {
                if (UserName != null)
                {
                    return Membership.GetUser(UserName);
                }
                return null;
            }
        }

        //Properties looked up from MembershipUser

        public string UserName
        {
            get
            {
                if (_membershipUser != null)
                {
                    return _membershipUser.UserName;
                }
                return string.Empty;
            }

        }

        public string Email
        {
            get
            {
                if (_membershipUser != null)
                {
                    return _membershipUser.Email;
                }
                return string.Empty;
            }

        }

        public object ProviderUserKey
        {
            get
            {
                if (_membershipUser != null)
                {
                    return _membershipUser.ProviderUserKey;
                }
                return null;
            }
        }


        #endregion

     }

}

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

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

发布评论

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

评论(2

原来是傀儡 2024-08-29 11:23:14

代码,看来你打算做很多不必要的工作。
没有令人信服的理由将成员资格和配置文件基础结构聚合在包装类中,并且有很多理由不这样做。

第一个原因是你不能在任何地方使用它。您将必须构建自己的基础设施。我已经做到了,但这并不有趣。

有趣的是对已建立的 API 进行编码,并利用投入数百万美元和数千工时来设计、开发和测试基于 ASP.NET 提供程序的系统。

如果您希望拥有自定义会员资格用户,第一步是从 MembershipUser 继承,以便您可以轻松实现直接插入 asp.net 框架的自定义提供程序。

请参阅此处< /a> 一个非常快速且易于理解的示例和 此处查看标准文档。

至于在您的用户中撰写个人资料。成员资格、角色和配置文件是基础架构的独立但相关的方面,并且在逻辑上进行了分离,以最大限度地提高灵活性和重用性。

我确信您会发现学习为基于提供者的 API 编写代码并专注于解决有趣的问题比重新发明轮子更有价值。

祝你好运

Code, It looks like you are planning to do a lot of unnecessary work.
And there is no compelling reason to aggregate the membership and profile infrastructure in a wrapper class and many reasons not to.

The first reason is that you can't use it anywhere. You will have to build your own infrastructure. I have done it and it aint fun.

What is fun is coding to an established api and taking advantage of the millions of dollars and thousands of man hours put into designing, developing and testing the provider based systems of ASP.NET.

If you wish to have a custom membership user the first step is to inherit from MembershipUser so that you can easily implement a custom provider that plugs right into the asp.net framework.

See here for a very quick and easily digested example and here for the standard docs.

As far as composing profile within your user. Membership, Roles and Profile are separate but related facets of the infrastructure and have been logically separated to maximize flexibility and reuse.

I am certain that you will find more value in learning to code to the provider based api and focusing on solving interesting problems than re-inventing the wheel.

Good luck

昔日梦未散 2024-08-29 11:23:14

您必须实现自己的自定义成员资格提供程序来填充它以及。

You'll have to implement your own Custom Membership Provider to populate it as well.

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