Passing ASP.NET User by Dependency Injection
Posted
by UpTheCreek
on Stack Overflow
See other posts from Stack Overflow
or by UpTheCreek
Published on 2010-05-13T05:48:26Z
Indexed on
2010/05/13
5:54 UTC
Read the original article
Hit count: 264
In my web application I have various components that need to access the currently authenticated user (HttpContext.User).
There are two obvious ways a component can access this: 1) Accessing getting the User from HttpContext.Current 2) Passing the user around in constructors
- Is not ideal because it makes testing difficult and ties application components to web concerns, when they really shouldn't know about it.
- Is just messy and complicates everything.
So I've been thinking about passing in the current user (or perhaps just the name/id) to any component that needs it using an IoC container (via dependency injection).
Is anyone using this technique to supply the current ASP.NET user to parts of the application? Or, Does this sound like a sensible approach? I would like know how this has worked out for people.
Thanks
© Stack Overflow or respective owner