简体   繁体   English

如何在ASP.NET MVC中获取另一个用户的配置文件?

[英]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.

相关问题 如何在Asp.Net Mvc中编辑用户配置文件,例如用户角色? - How to edit user profile such as user role in Asp.Net Mvc? 如何在ASP.NET MVC3剃刀视图中获取用户的成员资格资料? - How do you get the membership profile of a user in an ASP.NET MVC3 razor view? 如何从ASP.NET MVC自定义IdentityUser模型获取用户配置文件数据? - How to get user profile data from ASP.NET MVC custom IdentityUser Model? 如何在asp.net mvc 3中通过userId获取配置文件信息? - How to get profile info by userId in asp.net mvc 3? 在asp.net mvc 中获取用户的会话ID - Get user's session ID in asp.net mvc 如何在asp.net mvc中获取用户的本地系统日期时间? - How to get user's local sytem's datetime in asp.net mvc? Asp.Net Mvc编辑具有用户角色的用户配置文件 - Asp.Net Mvc Edit User profile with user Role 使用ASP.NET MVC和OAuthWebSecurity检索LinkedIn用户的配置文件时出错 - Error retrieving LinkedIn user's profile using ASP.NET MVC and OAuthWebSecurity 在 ASP.NET MVC 中使用外部身份验证获取所有服务的用户配置文件照片 - Get User Profile Photo For All Services using External Authentication in ASP.NET MVC Asp.Net Mvc 5 Azure Active Directory在服务器上获取并保存用户配置文件图像 - Asp.Net Mvc 5 Azure Active Directory Get and save user profile image on server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM