/// <summary> /// Retrieves user information for the given user from authentication store and sets the property values /// with values retrieved from database /// </summary> /// <remarks>Set the ZentityUser's logon name and password to correct values before calling this method</remarks> /// <exception cref="System.ArgumentException">Thrown when this method is called without setting LogOnName and Password</exception> /// <example> /// <code> /// try /// { /// ZentityUser user = new ZentityUser { LogOnName = "JohnDE" }; /// user.SetPassword("john@123"); /// user.FillUserProperties(); /// Console.WriteLine("FirstName : {0}", user.FirstName); /// Console.WriteLine("MiddleName : {0}", user.MiddleName); /// Console.WriteLine("LastName : {0}", user.LastName); /// Console.WriteLine("Email : {0}", user.Email); /// Console.WriteLine("City : {0}", user.City); /// Console.WriteLine("State : {0}", user.State); /// Console.WriteLine("Country : {0}", user.Country); /// Console.WriteLine("AccountStatus : {0}", user.AccountStatus); /// Console.WriteLine("Account Creation Date : {0}", user.DateCreated); /// Console.WriteLine("Account Modification Date : {0}", user.DateModified); /// Console.WriteLine("Password Creation Date : {0}", user.PasswordCreationDate); /// } /// catch (AuthenticationException ex) /// { /// Console.WriteLine(ex.Message); /// if (ex.InnerException != null) /// { /// Console.WriteLine(ex.InnerException.Message); /// } /// } /// /// </code> /// </example> public void FillUserProperties() { if (this.IsAuthenticated()) { ZentityUserProfile profile = DataAccessLayer.GetUserProfile(this.LogOnName); this.Profile = profile; } }
/// <summary> /// Initializes a new instance of the ZentityUser class using the logon /// name of the user, his authenticated token and his profile. /// </summary> /// <param name="logOnName">LogOn name of the user</param> /// <param name="userToken">Authenticated token of the logged on user.</param> /// <param name="profile">User's profile. Assigned to Profile property if parameter passed is non-null</param> public ZentityUser(string logOnName, AuthenticatedToken userToken, ZentityUserProfile profile) : this(logOnName, userToken) { if (profile != null) { this.Profile = profile; this.Profile.LogOnName = logOnName; } }
/// <summary> /// Initializes a new instance of the ZentityUser class using /// the credentials and profile parameters /// </summary> /// <param name="logOnName">User's logon name.</param> /// <param name="password">Clear text password.</param> /// <param name="profile">User's profile. Assigned to Profile property if parameter passed is non-null</param> /// <remarks>For a new user pass the chosen log on name and password for creating this instance.</remarks> public ZentityUser(string logOnName, string password, ZentityUserProfile profile) : this(logOnName, password) { if (profile != null) { this.Profile = profile; this.Profile.LogOnName = logOnName; } }
/// <summary> /// Returns a user profile /// </summary> /// <param name="logOnName">LogOnName of the user whose profile is needed</param> /// <returns>ZentityUser object filled with property values read from database</returns> /// <example> /// Pre-requisites for running this code sample /// <list type="bullet"> /// <item>Refer to the sample application configuration file given in help, and create a similar one for your application.</item> /// <item>Add reference to Zentity.Security.Authentication.dll and Zentity.Security.AuthenticationProvider.dll </item> /// <item>Run the sample for registering new users to create the user accounts in the authentication database.</item> /// <item>Then run this sample, replacing inputs with valid values</item> /// </list> /// <code> /// try /// { /// //Create a ZentityUserAdmin instance using built in or other existing administrator credentials. /// ZentityUserAdmin admin = new ZentityUserAdmin("Administrator", "XXXX");//Supply correct password /// ZentityUser user = admin.GetUserProfile("JohnDE"); /// //The ZentityUser instance contains all property values filled in from his record in the store. /// if (user != null) /// { /// Console.WriteLine("FirstName: {0}, AccountStatus: {1}", user.FirstName, user.AccountStatus); /// Console.WriteLine("Email: {0}", user.Email); /// } /// /// } /// //AuthenticationException might be thrown in case of errors in connecting to the authentication store /// //or if admin credentials are incorrect. /// catch (AuthenticationException ex) /// { /// Console.WriteLine(ex.Message); /// //In case of database errors the AuthenticationException object will wrap the sql exception. /// if (ex.InnerException != null) /// { /// Console.WriteLine(ex.InnerException.Message); /// } /// } /// /// </code> /// </example> public ZentityUserProfile GetUserProfile(string logOnName) { #region Input Validation if (string.IsNullOrEmpty(logOnName)) { throw new ArgumentNullException("logOnName"); } #endregion ZentityUserProfile profile = DataAccessLayer.GetUserProfile(logOnName); return(profile); }
/// <summary> /// Returns user profiles for the given range, sorted by LogOnName. /// </summary> /// <param name="startIndex">Start index for paged retrieval of users. First record has index 1. </param> /// <param name="endIndex">End index for paged retrieval of users. End index is inclusive - meaning startIndex = 1 and /// endIndex = 1 will return record at position 1. </param> /// <returns>List of ZentityUserProfile instances filled with property values from database records.</returns> internal static IEnumerable <ZentityUserProfile> GetUserProfiles(int startIndex, int endIndex) { #region Parameter validation if (startIndex <= 0 || endIndex == 0) { throw new ArgumentException(ConstantStrings.IndexValuesExceptionMessage); } if (startIndex > endIndex && endIndex != -1) { throw new ArgumentException(ConstantStrings.IndexValuesExceptionMessage); } #endregion //// Call SP GetPagedUserRecords try { Collection <ZentityUserProfile> pagedUsers = new Collection <ZentityUserProfile>(); //// Execute stored procedure GetUserInfo using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(ExecuteGetPagedUserRecordsFunction, conn)) { cmd.CommandType = CommandType.Text; SetCommandParameter(cmd, startIndex, endIndex); //// Execute stored procedure conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { //// Read the records one by one and create a Zentity user instance //// Fill in list of users ZentityUserProfile profile = SetUserProperties(reader); pagedUsers.Add(profile); } } } return(pagedUsers.AsEnumerable()); } catch (SqlNullValueException ex) { throw new AuthenticationException(ConstantStrings.DatabaseExceptionMessage, ex); } catch (SqlException ex) { throw new AuthenticationException(ConstantStrings.DatabaseExceptionMessage, ex); } }
/// <summary> /// Updates a user logon name. /// </summary> /// <param name="newLogOn">New logon name. This name should not be already in use.</param> /// <returns>True if user logon name is updated successfully</returns> /// <remarks>Set logon name, password of the ZentityUser object and then call this method.</remarks> /// <exception cref="System.ArgumentNullException">Thrown when newLogOn parameter is null or empty</exception> /// <exception cref="System.ArgumentException">Thrown when this method is called without first setting the /// logon name and password of the ZentityUser object.</exception> /// <example> /// <code> /// try /// { /// //For updating the logon name create an instance of ZentityUser and set logon name and password. /// //Then call the UpdateLogOnName method. /// ZentityUser user = new ZentityUser { LogOnName = "JohnDE" }; /// user.SetPassword("john@123"); /// bool isLogOnUpdated = user.UpdateLogOnName("john"); /// if (isLogOnUpdated) /// { /// Console.WriteLine("LogOn updated"); /// } /// else /// { /// //LogOn name might not be updated if the new logon chosen is already in use. /// Console.WriteLine("Errors in updating logon. The logon name might be in use. Try choosing a different logon name."); /// } /// } /// /// </code> /// </example> public bool UpdateLogOnName(string newLogOn) { //// Validations ValidateParameters("newLogOn", newLogOn); if (this.IsAuthenticated()) { if (ZentityUserProfile.ValidateLogOnName(newLogOn)) { bool success = DataAccessLayer.UpdateLogOnName(this.LogOnName, newLogOn); return success; } } return false; }
/// <summary> /// Resets password for a user who has forgotten both his password as well as security question and answer. /// </summary> /// <param name="logOnName">Log on name</param> /// <returns>List of available account status values.</returns> public string ResetPassword(string logOnName) { #region Parameter Validation if (string.IsNullOrEmpty(logOnName)) { throw new ArgumentNullException("logOnName"); } #endregion ZentityUserProfile userProfile = DataAccessLayer.GetUserProfile(logOnName); if (userProfile != null) { string newPassword = PasswordManager.ForgotPassword( userProfile.LogOnName, userProfile.SecurityQuestion, userProfile.Answer); return(newPassword); } return(string.Empty); }
/// <summary> /// Returns ZentityUser instance filled with property values from the database. /// </summary> /// <param name="logOnName">Logon name of the user.</param> /// <returns>ZentityUserProfile instance filled with property values stored in the database.</returns> internal static ZentityUserProfile GetUserProfile(string logOnName) { ZentityUserProfile profile = null; #region Parameter validation ValidateParameters("logOnName", logOnName); #endregion try { using (SqlConnection conn = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(ExecuteGetUserInfoFunction, conn)) { cmd.CommandType = CommandType.Text; SetCommandParameter(cmd, "LogOnName", logOnName); //// Execute stored procedure conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { profile = SetUserProperties(reader); } } } return(profile); } catch (SqlNullValueException ex) { throw new AuthenticationException(ConstantStrings.DatabaseExceptionMessage, ex); } catch (SqlException ex) { throw new AuthenticationException(ConstantStrings.DatabaseExceptionMessage, ex); } }
/// <summary> /// Reads records from the SqlDataReader, and fills in the ZentityUser instance with values read by the reader. /// </summary> /// <param name="reader">Sql data reader</param> /// <returns>Zentity user profile</returns> private static ZentityUserProfile SetUserProperties(SqlDataReader reader) { //// Parameter validation if (reader == null) { throw new ArgumentNullException("reader"); } if (reader.IsClosed) { throw new ArgumentException(ConstantStrings.ReaderClosedExceptionMessage); } string firstName = reader["FirstName"] as string; string middleName = reader["MiddleName"] as string; string lastName = reader["LastName"] as string; string logOnName = reader[4] as string; string city = reader["City"] as string; string state = reader["State"] as string; string country = reader["Country"] as string; string email = reader["Email"] as string; string accStatus = reader["AccountStatus"] as string; object dateCreated = reader["DateCreated"]; object dateModified = reader["DateModified"]; string ques = reader["SecurityQuestion"] as string; string ans = reader["Answer"] as string; object passwordCreationDate = reader["PasswordCreationDate"]; ZentityUserProfile profile = new ZentityUserProfile(); object id = reader["UserId"]; if (id != null && !string.IsNullOrEmpty(id.ToString())) { profile.Id = (Guid)id; } if (!string.IsNullOrEmpty(logOnName)) { profile.LogOnName = logOnName; } if (!string.IsNullOrEmpty(firstName)) { profile.FirstName = firstName; } if (!string.IsNullOrEmpty(middleName)) { profile.MiddleName = middleName; } if (!string.IsNullOrEmpty(lastName)) { profile.LastName = lastName; } if (!string.IsNullOrEmpty(email)) { profile.Email = email; } if (!string.IsNullOrEmpty(city)) { profile.City = city; } if (!string.IsNullOrEmpty(state)) { profile.State = state; } if (!string.IsNullOrEmpty(country)) { profile.Country = country; } if (!string.IsNullOrEmpty(accStatus)) { profile.AccountStatus = accStatus; } if (!string.IsNullOrEmpty(ques)) { profile.SecurityQuestion = ques; } if (!string.IsNullOrEmpty(ans)) { profile.SetHashedAnswer(ans); } if (dateCreated != null && !string.IsNullOrEmpty(dateCreated.ToString())) { profile.DateCreated = dateCreated as DateTime?; } if (dateModified != null && !string.IsNullOrEmpty(dateModified.ToString())) { profile.DateModified = dateModified as DateTime?; } if (passwordCreationDate != null && !string.IsNullOrEmpty(passwordCreationDate.ToString())) { profile.PasswordCreationDate = passwordCreationDate as DateTime?; } return(profile); }