Jared
Jared

Reputation: 6060

Relating data to an ASP.NET Member

I'm doing my first project based on ASP.NET Membership and I'm having issues with figuring out how to relate my custom data to a specific member. What is the convention here?

For example I'm wanting to make a web app where people can vote on different things. To ensure that people don't vote over and over again I'm wanting to track who has voted on what poll. (I'm providing an example of my intended use, but am mainly interested in a generic implementation)

Does anyone know of already written guides online or other good resources for this?

IF** it's relevant, I'm using EF4 for my db access (I don't directly need assistance with EF and/or db access only how the membership is related to other data). At the moment I'm planning on having the membership stored in MsSQL and the rest of the data in MySQL due to the space that GoDaddy gives me on each database.

Upvotes: 2

Views: 74

Answers (1)

moribvndvs
moribvndvs

Reputation: 42497

You need to uniquely identify the currently logged in user. It's most likely you'll want to use MembershipUser.ProviderUserKey, which is the unique identifier provided by the particular flavor of MembershipProvider. In the case of SqlMembershipProvider, the value of this property is a Guid.

So you can have a property on your vote record, like this (assuming you are using the SqlMembershipProvider):

public class Vote
{
    public Guid UserId { get; set;  }
    // etc.
}

You can get the current user's ID like this:

Guid userId = (Guid)Membership.GetUser().ProviderUserKey;

Vote vote = new Vote { UserId = userId };

Of course, from here you can query to make sure no Votes with UserId == userId exist before you create and save a new vote.

Upvotes: 5

Related Questions