[英]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. 我想在登录时设置一个用户时区的cookie。 AccountController.LogOn() seems like the best place to do this. AccountController.LogOn()似乎是最好的地方。 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. 用户完全登录后,上面的代码将返回正确的TimeZone。
One solution is to read the profile for the username trying to log on in AccountController.LogOn(): 一种解决方案是读取尝试在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: 除了Mathias的答案,您还可以将其转换为您键入的配置文件:
ProfileCommon profile = (ProfileCommon)ProfileBase.Create(username, true);
Also, the documentation for ProfileBase.Create is on MSDN. 此外, ProfileBase.Create的文档位于MSDN上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.