How to get another user's profile in ASP.NET MVC?
I want to set a cookie with a user's timezone when they login. AccountController.LogOn() seems like the best place to do this. However, I can't yet read a user's profile there since I guess you only have access to profiles when the method completes. So, this code returns an empty string:
Dim timeZone = HttpContext.Profile("TZ").ToString
Once a user has fully logged on, the above code returns the correct TimeZone.
One solution is to read the profile for the username trying to log on in AccountController.LogOn():
ProfileC开发者_如何学Common profile = Profile.GetProfile(username); // FAILS
However, this doesn't work.
So, how do I read a given user's profile if they're not logged in?
this doesnt look obvious but is a get Profile:
ProfileBase profile = ProfileBase.Create(HttpContext.Profile.UserName, true);
returns an existing instance.
In addition to Mathias' answer, you can then cast it to your typed profile:
ProfileCommon profile = (ProfileCommon)ProfileBase.Create(username, true);
Also, the documentation for ProfileBase.Create is on MSDN.
精彩评论