views:

91

answers:

1

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?

+4  A: 

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;
Gromer