ASP.net membership add custom column

孤者浪人 提交于 2019-12-21 05:41:08

问题


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!


回答1:


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."




回答2:


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




回答3:


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!




回答4:


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.



来源:https://stackoverflow.com/questions/3823037/asp-net-membership-add-custom-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!