How to reference the current user in the business layer

How to reference the current user in the business layer

Old forum URL: forums.lhotka.net/forums/t/7870.aspx


shrage posted on Sunday, October 25, 2009

I'm using the asp.net membership system, and in my business objects i want to keep track which UserID has updated the data, so i have a UserID column. in asp.net i can run code like this: Membership.Getuser().providerUserkey which will return the userID, but in csla i can only get the user using ApplicationContext.User.Identity.Name which means i will have to run an extra request to the database to get the user's id from its name, how can i avoid this?

JonnyBee replied on Sunday, October 25, 2009

Hi,

The ApplicationContext.User.Identity.Name is usually the UserID. For a Windows authenticated user it is domain\userid. Don't get confused abot the Name property - it is actually supposed to be the UserID.

If you need to you can create your own custom principal/identity objects and add your own properties.

shrage replied on Sunday, October 25, 2009

I have tried it out and it returns the user name not the id (which for the membership provider is a guid)

JoeFallon1 replied on Monday, October 26, 2009

I have custom Principal and Identity classes and have simply added some extra fields to carry around critical data about the logged in user. So I can cast the Principal object retrieved from AppContext to my custom Principal and simply read the extra value(s).

Joe

 

Copyright (c) Marimer LLC