TamerM
TamerM

Reputation: 762

Using SitecoreUser.Profile with Forms Authentication

We use sitecore to manage our registered users (extranet domain) and when creating new virtual users we give it an email using the Profile.Email property and then call the Profile.Save() method. Another property somewhere else reads the userProfile.Email, everything is fine at the beginning. Plus we use Forms authentication with the remember me feature. The problem is when we close the browser and reopen it Sitecore.Context.User contains info about the actual user who clicked remember me but the User.Profile always has the Email null. I tried Reload() and initialize() they don't work. I also tried getting the user again via the username (User.FromName()) but the returned user object also doesn't have the Profile Email.

What is being done wrong?

Upvotes: 2

Views: 639

Answers (1)

Anton
Anton

Reputation: 9961

There is one very important remark in Security API Cookbook. It is related to Sitecore 6 but as far as I know it should work with Sitecore 8 as there was no important changes in Security model. It worked for Sitecore 7.

Important You must log in a virtual user only after you assign Roles and Profile properties to them. The Roles and Profile properties that are assigned after logging in are lost upon subsequent request.

Sitecore.Security.Accounts.User user =
Sitecore.Security.Authentication.AuthenticationManager.BuildVirtualUser(@"domain\user"
, true);
if (user != null)
{
 string domainRole = @"domain\role";
 if (Sitecore.Security.Accounts.Role.Exists(domainRole))
 {
 user.Roles.Add(Role.FromName(domainRole));
 }

 user.Profile.Email = "[email protected]";
 user.Profile[“Custom Property”] = “Custom Value”;
 user.Profile.Save();
 Sitecore.Security.Authentication.AuthenticationManager.LoginVirtualUser(user);
}

Upvotes: 2

Related Questions