A common problem in asp.net is using the membership items that ship with asp and using a User in relationships to the other database tables you have, as a foreign key (to the User table and so, you need to get the User Guid for use in Creates or Edits in your code.

Fyi, the out-of-the-box asp authentication is FormsAuthentication.

Note that the User has a primary key of type uniqueidentifier (in Sql Server terms) which is a Guid in C# terms.
So, your foreign key column must be of type uniqueidentifier.

The way to get the Guid of the currently logged-in user is:

var mu = System.Web.Security.Membership.GetUser();
Guid userKey = (Guid)mu.ProviderUserKey;

This gets you the Guid of the currently logged-in user, and the way to check if a user is logged-in is by using: if (User.Identity.IsAuthenticated)

This works in ASP.NET 4 or MVC3 (all these versions now confuse me)

Advertisements