Example #1
0
        /// <summary>
        /// Updates log on name of the current logged on user.
        /// </summary>
        /// <param name="token">AuthenticatedToken of the logged on user.</param>
        /// <param name="currentLogOn">Current logon name of the user whose logon name is to be updated.</param>
        /// <param name="newLogOn">New log on name.</param>
        /// <returns>True if operation succeeds.</returns>
        /// <remarks>An administrator can update any user's log on name. Other users can update only their own logon name.
        /// Hence for non-admin users the current log on must match the logged on user's log on name.</remarks>
        /// <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 System.IdentityModel, 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>
        /// //Get authentication provider from factory
        /// IAuthenticationProvider provider = AuthenticationProviderFactory.CreateAuthenticationProvider(&quot;ZentityAuthenticationProvider&quot;);
        /// //Login as admin
        /// AuthenticatedToken adminToken = provider.Authenticate(new UserNameSecurityToken(&quot;Administrator&quot;, &quot;XXXX&quot;));//Supply correct password
        /// //Administrator changes Jimmy's log on name.
        /// bool updated = ZentityUserManager.UpdateLogOnName(adminToken, &quot;Jimmy&quot;, &quot;Jimmy12&quot;);
        /// if (updated)
        /// {
        ///     Console.WriteLine(&quot;Updated log on name successfully&quot;);
        /// }
        /// else
        /// {
        ///     Console.WriteLine(&quot;Could not update log on name.&quot;);
        /// }
        /// </code>
        /// </example>
        /// <seealso cref="Zentity.Security.AuthenticationProvider.ZentityAuthenticationProvider"/>
        public static bool UpdateLogOnName(AuthenticatedToken token, string currentLogOn, string newLogOn)
        {
            #region Input Validation
            ValidateToken(token);
            ValidateParameters("currentLogOn", currentLogOn, "newLogOn", newLogOn);
            #endregion

            try
            {
                //// The token must be of the user who is updating the logon name or it must be an admin's token.
                if (!string.Equals(token.IdentityName, currentLogOn, StringComparison.OrdinalIgnoreCase) &&
                    !DataAccessLayer.IsAdmin(token.IdentityName))
                {
                    return(false);
                }

                ZentityUser user    = new ZentityUser(currentLogOn, token);
                bool        success = user.UpdateLogOnName(newLogOn);
                return(success);
            }
            catch (TypeInitializationException ex)
            {
                //// thrown in case of incorrect application configuration
                throw new AuthenticationException(ConstantStrings.TypeInitializationExceptionMessage, ex);
            }
        }
Example #2
0
        /// <summary>
        /// Updates a user logon name.
        /// </summary>
        /// <param name="currentLogOn">Current log on name</param>
        /// <param name="newLogOn">New log on name</param>
        /// <param name="password">User Password</param>
        /// <returns>True if user logon name is updated successfully</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
        ///    {
        ///        bool isLogOnUpdated = ZentityUserManager.UpdateLogOnName(&quot;JohnDE&quot;, &quot;john&quot;, &quot;john@123&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;);
        ///        }
        ///    }
        ///    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 static bool UpdateLogOnName(string currentLogOn, string newLogOn, string password)
        {
            #region Input Validation
            ValidateParameters("currentLogOn", currentLogOn, "newLogOn", newLogOn, "password", password);
            #endregion

            try
            {
                ZentityUser user    = new ZentityUser(currentLogOn, password);
                bool        success = user.UpdateLogOnName(newLogOn);
                return(success);
            }
            catch (TypeInitializationException ex)
            {
                //// thrown in case of incorrect application configuration
                throw new AuthenticationException(ConstantStrings.TypeInitializationExceptionMessage, ex);
            }
        }