Pablo Fernandez
Pablo Fernandez

Reputation: 287400

How do you refer to a membership provider user in a SQL table?

When I create SQL tables and I want to refer to a membership provider user in ASP.NET, what type of field do I use and how do I get the value from the user?

Upvotes: 0

Views: 210

Answers (1)

Gromer
Gromer

Reputation: 9931

In SQL Server, you'd use UNIQUEIDENTIFIER as the column type and map it to [dbo].[aspnet_Users].[UserId]. This is assuming you're using the default SqlMembership in the ASP.Net application. To get the logged in user's UserId in C#, you'd use:

MembershipUser mu = Membership.GetUser();  // This gets the CURRENTLY logged in user.
MembershipUser mu = Membership.GetUser("username");  // This gets the user with username username.
// Obviously you'd use only one of the above 2 lines, otherwise you'd get an error about mu being declared twice.
Guid currentUserId = (Guid)mu.ProviderUserKey;

Upvotes: 4

Related Questions