Sitecore asp.net编辑自定义用户配置文件属性

时间:2014-11-14 17:08:05

标签: c# asp.net sitecore sitecore7

我在用户个人资料上有一个字段,该字段对应于另一个系统中的id。我想在用户访问激活链接时设置此项。在其他系统中注册用户后,我添加一个角色并将其成员资格批准设置为true。我收到一个错误,说我无法设置匿名用户的属性,所以我移动了在角色/激活部分之后分配id但我仍然遇到相同的错误。

private bool SetCustomId(string username, Guid id)
{

    var userProfile = ((User)User.FromName(username, AccountType.User)).Profile;
    userProfile.SetCustomProperty("MyIdField", id.ToString());
    userProfile.Save();//error thrown here
}

用户个人资料是我所期望的,当我在为用户分配角色的部分之后移动它时,他们确实具有我期望的角色。

以下是实际例外:This property cannot be set for anonymous users.

3 个答案:

答案 0 :(得分:4)

请尝试使用User.FromName(username, true)。 boolean参数告诉它将用户视为已验证。

答案 1 :(得分:3)

您需要初始化用户,请尝试以下代码:

userProfile.Initialize(username, true);

答案 2 :(得分:0)

试试这个:

private bool SetCustomId(string username, Guid id)
{
    using(new Sitecore.Security.Accounts.UserSwitcher(Sitecore.Security.Accounts.User.FromName(username,false));)
    {
        var userProfile = ((User)User.FromName(username, AccountType.User)).Profile;
        userProfile.SetCustomProperty("MyIdField", id.ToString());
        userProfile.Save();
    }

}
相关问题