I wonder if SimpleMembership keeps UserId somewhere, or if not, how to force it to keep, once derived data.
I have simple action:
[Authorize(Roles="User")]
public ActionResult Create()
{
var ownerId = WebSecurity.GetUserId(User.Identity.Name);
return View()
}
And my MiniProfiler says:
DECLARE @0 nvarchar(4) = N'NICK'
SELECT [UserId] FROM [UserProfile] WHERE (UPPER([UserName]) = @0)
DECLARE @0 int = 2
SELECT r.RoleName FROM webpages_UsersInRoles u, webpages_Roles r Where
(u.UserId = @0 and u.RoleId = r.RoleId) GROUP BY RoleName
DECLARE @0 nvarchar(4) = N'NICK'
SELECT [UserId] FROM [UserProfile] WHERE (UPPER([UserName]) = @0)
So, I have duplicated query for UserId – first to get the role, second by calling WebSecurity.GetUserId(User.Identity.Name);. Is there a smart way to save UserId somewhere after calling [Authorize] ?
I usually wrap my users profiles in a session variable.
after the user logins in query the
UserProfiletable and doonce there you can access it any time by doing
I have the
userProfiletable as an object class in my applicationEdit
I have this code that gets executed after a user logins
I’m using EF5 and I have the
userProfiletable in my model, so you’ll need to adapt this part if you’re not using EF.When I need the
userIdI just doSessionHelpers.UserInfo.userId. This let’s me get all of the users profile info whenever I need it without requerying the database.Of course if you change something in the user’s profile you will need to update your session variable as well.
You may need to have more advanced logic and check that the session isn’t null (which I’m doing elsewhere).