简体   繁体   中英

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():

ProfileCommon 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM