Exemple #1
0
 /// <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 = &quot;JohnDE&quot; };
 ///        user.SetPassword(&quot;john@123&quot;);
 ///        user.FillUserProperties();
 ///        Console.WriteLine(&quot;FirstName : {0}&quot;, user.FirstName);
 ///        Console.WriteLine(&quot;MiddleName : {0}&quot;, user.MiddleName);
 ///        Console.WriteLine(&quot;LastName : {0}&quot;, user.LastName);
 ///        Console.WriteLine(&quot;Email : {0}&quot;, user.Email);
 ///        Console.WriteLine(&quot;City : {0}&quot;, user.City);
 ///        Console.WriteLine(&quot;State : {0}&quot;, user.State);
 ///        Console.WriteLine(&quot;Country : {0}&quot;, user.Country);
 ///        Console.WriteLine(&quot;AccountStatus : {0}&quot;, user.AccountStatus);
 ///        Console.WriteLine(&quot;Account Creation Date : {0}&quot;, user.DateCreated);
 ///        Console.WriteLine(&quot;Account Modification Date : {0}&quot;, user.DateModified);
 ///        Console.WriteLine(&quot;Password Creation Date : {0}&quot;, 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;
     }
 }
Exemple #2
0
 /// <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;
     }
 }
Exemple #3
0
 /// <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(&quot;Administrator&quot;, &quot;XXXX&quot;);//Supply correct password
        ///        ZentityUser user = admin.GetUserProfile(&quot;JohnDE&quot;);
        ///        //The ZentityUser instance contains all property values filled in from his record in the store.
        ///        if (user != null)
        ///        {
        ///            Console.WriteLine(&quot;FirstName: {0}, AccountStatus: {1}&quot;, user.FirstName, user.AccountStatus);
        ///            Console.WriteLine(&quot;Email: {0}&quot;, 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);
        }
Exemple #5
0
        /// <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);
            }
        }
Exemple #6
0
        /// <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 = &quot;JohnDE&quot; };
        ///        user.SetPassword(&quot;john@123&quot;);
        ///        bool isLogOnUpdated = user.UpdateLogOnName(&quot;john&quot;);
        ///        if (isLogOnUpdated)
        ///        {
        ///            Console.WriteLine(&quot;LogOn updated&quot;);
        ///        }
        ///        else
        ///        {
        ///            //LogOn name might not be updated if the new logon chosen is already in use.
        ///            Console.WriteLine(&quot;Errors in updating logon. The logon name might be in use. Try choosing a different logon name.&quot;);
        ///        }
        ///    }
        ///
        /// </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);
        }
Exemple #8
0
        /// <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);
            }
        }
Exemple #9
0
        /// <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);
        }