this class provides you with information about the user.
using Centralpoint.Web;
using System;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Xml;
namespace Centralpoint.WebSite
{
public class UserInfo
{
private CpCollection _personalization, _configuration, _properties;
private Guid[] _navigationItemIds, _roleIds, _audienceIds, _cdaAudienceIds;
public Guid UserId { get; set; }
public Guid ParentUserId { get; set; }
public string UserName { get; set; }
public string PasswordHash { get; set; }
[Obsolete("This property has been replaced by PasswordHash.")]
public string Password { get; set; } = "";
[Obsolete("This property has been replaced by PasswordHash.")]
public string PasswordSalt { get; set; } = "";
public string Email { get; set; }
public bool IsLockedOut { get; set; }
public DateTime LastLoginDate { get; set; }
public DateTime LastPasswordChangedDate { get; set; }
public int FailedPasswordAttemptCount { get; set; }
public string PersonalizationXml { get; set; }
public string ConfigurationXml { get; set; }
public string PropertiesXml { get; set; }
public string IntegrationId { get; set; }
public bool IsDeleted { get; set; }
public bool IsSystem { get; set; }
public DateTime CreateDate { get; set; }
public DateTime ModifyDate { get; set; }
public bool IsUpdateable { get; set; }
public string DisplayName { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool DynamicRoles { get; set; }
public string GeoCode { get; set; }
public bool IsLocal { get; set; }
public Guid AuthenticationSourcesDataId { get; set; }
public string GroupId { get; set; }
public UserInfo(string userName, string email, string password, Guid userId)
{
DateTime now = DateTime.UtcNow;
this.UserId = userId;
this.ParentUserId = Guid.Empty;
this.UserName = userName;
this.PasswordHash = PasswordHasher.HashPassword(password);
this.Email = email;
this.IsLockedOut = false;
this.LastLoginDate = DateTime.MinValue;
this.LastPasswordChangedDate = now;
this.FailedPasswordAttemptCount = 0;
this.PersonalizationXml = String.Empty;
this.ConfigurationXml = String.Empty;
this.PropertiesXml = String.Empty;
this.IntegrationId = String.Empty;
this.IsDeleted = false;
this.IsSystem = false;
this.CreateDate = now;
this.ModifyDate = DateTime.MinValue;
this.DisplayName = String.Empty;
this.StartDate = DateTime.UtcNow;
this.EndDate = Utilities.General.SqlMaxDate;
this.DynamicRoles = false;
this.GeoCode = "";
this.IsUpdateable = true;
this.IsLocal = true;
this.AuthenticationSourcesDataId = Guid.Empty;
this.GroupId = String.Empty;
}
public UserInfo(string userName, string email, string password)
: this(userName, email, password, Guid.NewGuid())
{
}
public UserInfo(Guid userId)
{
this.UserId = userId;
GetUserInfo($"UserId = '{this.UserId}'");
}
internal UserInfo(string filter)
{
if (String.IsNullOrWhiteSpace(filter)) throw new Exception("Parameters to identify a user is missing.");
GetUserInfo(filter);
}
private void GetUserInfo(string filter)
{
using (SqlConnection connection = new SqlConnection(Management.Application.SelectConnectionString))
{
SqlCommand command = new SqlCommand($"SELECT TOP 1 ParentUserId, UserName, PasswordHash, '' AS PasswordSalt, Email, IsLockedOut, LastLoginDate, LastPasswordChangedDate, FailedPasswordAttemptCount, Personalization, Configuration, Properties, IntegrationId, IsDeleted, IsSystem, CreateDate, ModifyDate, DisplayName, StartDate, EndDate, DynamicRoles, GeoCode, IsLocal, AuthenticationSourcesDataId, UserId, GroupId FROM cpsys_Users WHERE {filter}", connection);
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
this.ParentUserId = reader.IsDBNull(0) ? Guid.Empty : reader.GetGuid(0);
this.UserName = reader.GetString(1);
this.PasswordHash = reader.GetString(2);
this.Email = reader.GetString(4);
this.IsLockedOut = reader.GetBoolean(5);
this.LastLoginDate = reader.IsDBNull(6) ? DateTime.MinValue : reader.GetDateTime(6);
this.LastPasswordChangedDate = reader.GetDateTime(7);
this.FailedPasswordAttemptCount = reader.GetInt32(8);
this.PersonalizationXml = reader.IsDBNull(9) ? String.Empty : reader.GetString(9);
this.ConfigurationXml = reader.IsDBNull(10) ? String.Empty : reader.GetString(10);
this.PropertiesXml = reader.IsDBNull(11) ? String.Empty : reader.GetString(11);
this.IntegrationId = reader.IsDBNull(12) ? String.Empty : reader.GetString(12);
this.IsDeleted = reader.GetBoolean(13);
this.IsSystem = reader.GetBoolean(14);
this.CreateDate = reader.GetDateTime(15);
this.ModifyDate = reader.IsDBNull(16) ? DateTime.MinValue : reader.GetDateTime(16);
this.DisplayName = reader.IsDBNull(17) ? String.Empty : reader.GetString(17);
this.StartDate = reader.GetDateTime(18);
this.EndDate = reader.GetDateTime(19);
this.DynamicRoles = reader.GetBoolean(20);
this.GeoCode = reader.IsDBNull(21) ? String.Empty : reader.GetString(21);
this.IsUpdateable = false;
this.IsLocal = reader.GetBoolean(22);
this.AuthenticationSourcesDataId = reader.IsDBNull(23) ? Guid.Empty : reader.GetGuid(23);
this.UserId = reader.GetGuid(24);
this.GroupId = reader.IsDBNull(25) ? String.Empty : reader.GetString(25);
}
else
throw new Exception(String.Format("The {0} user could not be found.", this.UserId));
}
}
}
public CpCollection Personalization
{
get
{
if (_personalization != null) return _personalization;
_personalization = new CpCollection();
_personalization.LoadFromFile(HttpContext.Current.Server.MapPath(Centralpoint.Web.UI.CpPage.GetWebResourceUrl("Personalization.xml")));
if (!String.IsNullOrEmpty(PersonalizationXml)) _personalization.Load(PersonalizationXml);
return _personalization;
}
}
public CpCollection Configuration
{
get
{
if (_configuration != null) return _configuration;
_configuration = new CpCollection();
_configuration.LoadFromFile(HttpContext.Current.Server.MapPath("/App_Data/ConfigurationUser.xml"));
if (!String.IsNullOrEmpty(ConfigurationXml)) _configuration.Load(ConfigurationXml);
return _configuration;
}
}
public CpCollection Properties
{
get
{
if (_properties != null) return _properties;
_properties = new CpCollection();
// initial properties are only loaded when the user is updateable
if (IsUpdateable)
{
string baseFile = HttpContext.Current.Server.MapPath("/App_Data/PropertiesUser.xml");
string file = Properties.Get("UserPropertiesFile").Trim();
if (String.IsNullOrEmpty(file))
_properties.LoadFromFile(baseFile);
else
{
file = HttpContext.Current.Server.MapPath(file);
if (!File.Exists(file))
_properties.LoadFromFile(baseFile);
else
{
XmlDocument doc = new XmlDocument();
doc.Load(file);
doc = CpCollection.MergeXml(baseFile, doc.OuterXml, true);
_properties.Load(doc.OuterXml);
}
}
}
if (!String.IsNullOrEmpty(PropertiesXml)) _properties.Load(PropertiesXml);
return _properties;
}
}
public Guid[] NavigationItemIds
{
get
{
if (_navigationItemIds == null)
_navigationItemIds = Utilities.Database.SelectGuidArray(Management.Application.SelectConnectionString
, String.Format("SELECT NavigationItemId FROM cpsys_UsersInNavigationItems WHERE UserId = '{0}'", UserId), "NavigationItemId");
return _navigationItemIds;
}
set { _navigationItemIds = value; }
}
public Guid[] RoleIds
{
get
{
if (_roleIds == null)
_roleIds = Utilities.Database.SelectGuidArray(Management.Application.SelectConnectionString
, String.Format("SELECT RoleId FROM cpsys_UsersInRoles WHERE UserId = '{0}'", UserId), "RoleId");
return _roleIds;
}
set { _roleIds = value; }
}
public Guid[] AudienceIds
{
get
{
if (_audienceIds == null)
_audienceIds = Utilities.Database.SelectGuidArray(Management.Application.SelectConnectionString
, String.Format("SELECT AudienceId FROM cpsys_UsersInAudiences WHERE (UserId = '{0}') AND (IsConsole = 1)", UserId), "AudienceId");
return _audienceIds;
}
set { _audienceIds = value; }
}
public Guid[] CdaAudienceIds
{
get
{
if (_cdaAudienceIds == null)
_cdaAudienceIds = Utilities.Database.SelectGuidArray(Management.Application.SelectConnectionString
, String.Format("SELECT AudienceId FROM cpsys_UsersInAudiences WHERE (UserId = '{0}') AND (IsCda = 1)", UserId), "AudienceId");
return _cdaAudienceIds;
}
set { _cdaAudienceIds = value; }
}
}
}
example of using UserInfo
UserInfo author = new UserInfo(new Guid("57F140CE-D147-4AED-93D8-AA913179A4DF"));
string authorName = author.Properties.Get("ContactFirstName") + " " + author.Properties.Get("ContactLastName");
Keywords: user