更新 ASP.NET 中现有用户的配置文件

发布于 2024-08-05 09:03:55 字数 82 浏览 10 评论 0原文

我想更改现有用户的配置文件,而不必为该用户创建新的配置文件。例如:我有一个用户名 Usr1,我只想更改他的年龄,我该怎么做?

I want to change an existing user's profile without having to create a new profile for the user. For example: I have a user name Usr1 and I want to change only his age, how do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的痛♀有谁懂 2024-08-12 09:03:56

当您进入该页面时,您可以使用 ProfileCommon 类来访问配置文件。 profilecommon 类是在 Web 项目编译期间由 ASP.NET 根据您的 web.config 配置文件设置自动生成的。

如果您想使用 app_code 文件夹中的配置文件,则必须使用 profilebase 类。页面中可用的 Profilecommon 也派生自此类。

Profilebase 可以像这样访问

HttpContext.Profile or HttpContext.Current.Profile

要读取配置文件值,您需要执行以下操作

HttpContext.Profile.GetPropertyValue("propertyName");

要将值写入您需要写入的配置文件

HttpContext.Profile.SetPropertyValue("propertyName", "propertyValue");

When you are in the page you have the ProfileCommon class available to you for accessing the profile. The profilecommon class is automatically generated by asp.net from you web.config profile settings during compilation of the web project.

In case you want to use the profile from app_code folder, you will have to use the profilebase class. Profilecommon that is available in the page also derives from this class.

Profilebase can be access like this

HttpContext.Profile or HttpContext.Current.Profile

To read a profile value you need to do the following

HttpContext.Profile.GetPropertyValue("propertyName");

To write a value to the profile you need to write

HttpContext.Profile.SetPropertyValue("propertyName", "propertyValue");
葬花如无物 2024-08-12 09:03:56

有关完整详细信息,请参阅本文文章。请注意,在某些情况下(请参阅我对另一个答案的评论),不会生成 ProfileCommon。

在这种情况下,您需要恢复使用 ProfileBase:

ProfileBase profile = context.Profile;
DateTime dob= profile.GetPropertyValue("dob") as DateTime;
...
profile.SetPropertyValue("dob",dob);

See this article for the full details. Be aware that in certain cases (see my comment to another answer), ProfileCommon is not generated.

In this case you need to revert to using ProfileBase:

ProfileBase profile = context.Profile;
DateTime dob= profile.GetPropertyValue("dob") as DateTime;
...
profile.SetPropertyValue("dob",dob);
挽清梦 2024-08-12 09:03:56

如果您尝试执行的是更新他人的用户个人资料(例如,您是输入客户用户名的管理员),则可以使用类似以下内容的内容。

Dim p As ProfileCommon = Profile.GetProfile("Usr1")

p.TestValue1 = "New Value"
p.TestValue2 = "Another New Value"

p.Save()

同样,如果您使用的是 Web 项目而不是网站,则必须使用 p.SetPropertyValue() 而不是强类型属性名称。

If what you are trying to do is update another's user profile (say, you are an admin who types in a customer's username), you can use something like the following.

Dim p As ProfileCommon = Profile.GetProfile("Usr1")

p.TestValue1 = "New Value"
p.TestValue2 = "Another New Value"

p.Save()

Again, if you are using a web project instead of a web site, you will have to use p.SetPropertyValue() instead of the strong-typed property names.

苯莒 2024-08-12 09:03:56

我有一个类似的问题并使用sql脚本搜索解决方案。这有点困难,但在服务器端代码不可行的情况下也是可行的。
我在 dbo.aspnet_Profile (PropertyValuesString, PropertyNames) 中设置了两个字段,

UPDATE dbo.aspnet_Profile 
SET 
PropertyValuesString = cast(Replace(cast(PropertyValuesString as nvarchar(max)),'New Value','Old Value') as ntext)
,PropertyNames='New calculated property names'
WHERE 
UserId='userID'

困难的部分是更改 PropertyNames 字段。它包含配置文件名称属性、起始位置和长度。类似的东西:地址:S:31:12
您必须相应地重新计算起始位置和长度新值。

I have a similar problem and search for solution with sql script. It is little bit more difficult but it is doable in case server side code is not an option.
I set two fields in dbo.aspnet_Profile (PropertyValuesString, PropertyNames)

UPDATE dbo.aspnet_Profile 
SET 
PropertyValuesString = cast(Replace(cast(PropertyValuesString as nvarchar(max)),'New Value','Old Value') as ntext)
,PropertyNames='New calculated property names'
WHERE 
UserId='userID'

Difficult part is to change PropertyNames field.It contains profile name property, start position, and length. Something like that : address:S:31:12
You have to recalculated start positions and lengths accordingly new value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文