How to Find the Profile of the Current User
This topic explores how to find the Commerce Server Core Systems profile for the currently logged on user. You do this by writing site code that retrieves the current Commerce Server Core Systems context in a CommerceContext object and retrieves the profile information for a given e-mail address and password.
The password retrieved from calling the PropertyGroups property of the profile retrieved from the GetProfile method is, by default, stored in an encrypted format. Therefore, you must validate the password entered by the user against the stored password in the database. For more information about password validation, see How to Validate Passwords.
To find the current user profile and validate the password
Create a CommerceContext object to obtain the current runtime context.
Call the GetProfile method to retrieve the profile, passing the values that you want to retrieve from the Profiles System.
Assign the resulting Profile object to a variable.
This variable will contain a PropertyGroups collection.
Retrieve the user_security_password property from the retrieved profile PropertyGroups collection and assign it to a string variable.
This represents the encrypted password stored in the Profiles System.
Determine whether the encrypted password is correct by passing it into the validatePassword function.
For more information about validating passwords, see How to Validate Passwords.
Example
This example shows a sample GetUserProfile function that illustrates how to write code to retrieve a user profile from the Profiles System, when the function is passed an e-mail address and password.
private void GetUserProfile(string emailAddress, string password)
{
try
{
// Get the current CommerceContext.
CommerceContext csContext = CommerceContext.Current;
// Return a profile after looking it up by e-mail_address.
Profile profile = csContext.ProfileSystem.GetProfile("GeneralInfo.email_address", emailAddress, "UserObject");
// Get encrypted password for current profile.
string encryptedPassword = profile.PropertyGroups["GeneralInfo"].Properties["user_security_password"].Value.ToString();
// Validate password.
if (validatePassword(password, encryptedPassword) == true)
{
csContext.UserID = emailAddress;
csContext.UserProfile = profile;
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
// Failure!
Console.WriteLine("Exception: {0}Message: {1}", ex.GetType(), ex.Message);
if (ex.InnerException != null)
{
Console.WriteLine("Inner Exception: {0}Message: {1}", ex.InnerException.GetType(), ex.InnerException.Message);
}
}
}
Compiling the Code
- To compile the code, you need to include these namespace directives:
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Profiles;