ASP.NET Membership Send username across different tiers
I have 3-tiers (UI, BLL, DAL), those BLL and DAL reside on external WS, obviously my membership user works on the UI tier.
So... I was trying to store in my Bussines tabl开发者_运维知识库es the username for the differents tasks (like a CRUD)
- Insert registry (username: nicolas)
- Delete registry (username: peter) etc...
The simplest way is send the username on ALL methods and store in the differents tables. But this idea is not scalable and very dirty if you have a lot of bussines tables.
Any know another way to do this? I read about writing a custom membership provider to store the differents user on cache and retrieve from this. But i don't know if this is the best workaround.
Any ideas?
The custom controls for ASP.NET rely on the Membership library in System.Web (more specifically, System.Web.Security). As long as you are using those controls (for example, the <asp:Login>
control) you already have a dependency on System.Web. In that case, any layer of your application can do something like this to retrieve the current user:
using System.Web.Security;
...
MembershipUser user = Membership.GetUser();
Then use user.UserName
to retrieve the user's username.
UPDATED to reflect the understanding that the layers are hosted on separate servers.
I would add a StartSession() method to the BLL that takes the username as a parameter, and returns a string session ID. The BLL on the server generates the session ID (perhaps using a Guid) and uses the BLL-side cache to save the username. Then on the client side, set a cookie that contains the session ID. It will get submitted along with the web request. (Depending on how you submit the web request, you may have to set the cookie value with javascript.)
That way you don't have to modify the signature of your methods, but can still get ahold of the username from the BLL by using the cache. You will have to make sure your login page calls the GetSession() method, and sets the session cookie before submitting more requests.
Unless you are using services in the BLL and DLL then what is the point, there is no access to these except through the UI.
精彩评论