如何加载特定用户的配置文件

时间:2013-09-05 10:24:57

标签: c# asp.net asp.net-mvc-4

我有一个用于创建新用户的MVC Controller操作。我正在尝试为新用户存储其他详细信息,例如FullName

问题是Profile适用于当前经过身份验证的用户,在我的情况下是admin。如何引用新创建的用户的配置文件?

var user = Membership.CreateUser(newUser.Username, newUser.Password, newUser.Email);
Profile.SetPropertyValue("fullname", newUser.FullName); //wrong - stored against the admin that is creating the user
user.??? //I do have the user, but can I get the profile?

1 个答案:

答案 0 :(得分:2)

创建用户会将其添加到用户列表中。但是,这不会对当前请求的新用户进行身份验证或授权。您还需要在当前请求上下文中对用户进行身份验证。用户创建后无法立即访问配置文件。

如果要在同一请求中为新创建的用户创建配置文件,则需要致电HttpContext.Current.Profile.Initialize(userName, true) HttpContext.Profile 会为您提供ProfileBase个对象,然后调用 Initialize() 方法将初始化传递给它的userName的配置文件。然后,您可以填充新初始化的配置文件并继续更改并保存。

在创建时需要立即创建/访问配置文件时,请使用HttpContext.Current.Profile。在其他请求中,您可以使用/继续 ProfileBase.Create(userName) 来获取指定用户名的配置文件实例。

但是,如果您在创建时立即对用户进行身份验证,您将能够以最简单的方式访问该配置文件。