ASP.net 会员添加自定义列

发布于 2024-09-25 17:59:20 字数 424 浏览 4 评论 0原文

在我的母版页中,我有:

MembershipUser thisUser = Membership.GetUser();
loggedInUserID = thisUser.ProviderUserKey.ToString();

thisUser 使我可以访问 aspnet_Membership 中的所有字段。

我想要为每个用户添加一个新字段 isSubscribed。我可以使用 SQL 查询来获取该值,但我想知道是否有某种方法可以修改membershipuser 对象,以便它也检索该值,因此可以从以下位置访问它:

thisUser.isSubscribed.ToString();

感谢您的帮助!

In my master page I have:

MembershipUser thisUser = Membership.GetUser();
loggedInUserID = thisUser.ProviderUserKey.ToString();

thisUser gives me access to all the fields in aspnet_Membership.

I want a new field, isSubscribed for each user. I can use an SQL query to fetch the value fine, but I want to know if there is someway to modify the membershipuser object so it retrieves this value as well, so it is accessible from:

thisUser.isSubscribed.ToString();

Thanks for any help!

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

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

发布评论

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

评论(4

污味仙女 2024-10-02 17:59:20

您需要将该字段添加到配置文件提供程序

。可以在此处找到配置文件提供程序的说明。

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

这里摘自文章

“ASP.NET 配置文件功能将信息与单个用户相关联,并以持久格式存储信息。配置文件允许您管理用户信息,而不需要您创建和维护自己的数据库此外,ASP.NET 配置文件功能使用强类型 API 提供用户信息,您可以从应用程序中的任何位置访问该信息。”

you will need to add the field to the Profile Provider

A description of the Profile provider can be found here.

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

here is an excerpt from the article

"The ASP.NET profile feature associates information with an individual user and stores the information in a persistent format. Profiles allow you to manage user information without requiring you to create and maintain your own database. In addition, the ASP.NET profile feature makes the user information available using a strongly typed API that you can access from anywhere in your application."

多情癖 2024-10-02 17:59:20

会员资格用于识别和认证。为了元属性而破解安全性并不是一个好的做法。

如前所述,Profile 是存储元数据的正确位置,这将消除对自定义 MembershipUser 的需要。

如果您需要 sql 查询访问数据,请使用 SqlTableProvider

Membership is for identification and authentication. It is not good practice to hack your security for the sake of a meta property.

As mentioned, Profile is the proper place to store meta data and this would obviate the need for a custom MembershipUser.

If you need sql query access to the data use the SqlTableProvider

铃予 2024-10-02 17:59:20

Si Robinson 给出了一个很好的答案,可以存储针对用户的附加元数据,而无需更改底层架构,但如果您已经在自定义数据库架构中存储了有关该用户的数据,那么这将不太有效。

我使用的解决方案是实现我自己的会员资格提供程序:

http://msdn。 microsoft.com/en-us/library/f1kyba5e.aspx

然后您可以实现自己的 MembershipUser ,它公开 IsSubscribed 属性。

这适用于 ASP.NET 中的成员资格流程,例如登录组件。您所需要做的就是将 GetUser() 返回的对象转换为您的自定义实现,然后就完成了!

Si Robinson gave a good answer for storing additional meta data against users without having to change the underlying schema but if you already have data stored about this user in your custom database schema, that won't quite work out.

The solution I have used is to implement my own membership provider:

http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx

And then you can implement your own MembershipUser which exposes the IsSubscribed property.

This works fine with the Membership process within ASP.NET such as the login components. All you need to do is cast the object returned by GetUser() to your custom implementation and you are set!

余生一个溪 2024-10-02 17:59:20

您可以为此使用角色并将用户分配给订阅者角色。例如:

Roles.AddUserToRole("Bob", "Subscriber");

您将有一个真正不有趣的时间通过个人资料字段进行查询。有了角色,您将能够枚举具有以下功能的用户:

Roles.GetUsersInRoles("Subscriber");

您将能够将这些角色添加到 Web.Config 文件中,以控制只有订阅者可以看到的网站部分。可能比使用基于配置文件字段的条件包装内容更好。

You could use roles for this and assign users to a Subscriber role. Such as:

Roles.AddUserToRole("Bob", "Subscriber");

You're gonna have a real un-fun time querying by profile fields. With a role you will be able to enumerate users with:

Roles.GetUsersInRoles("Subscriber");

And you'll be able to add these roles to Web.Config files to control which parts of the site only Subscribers can see. Possibly better than wrapping content with a conditional based on a profile field.

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