/// <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("ZentityAuthenticationProvider"); /// //Login as admin /// AuthenticatedToken adminToken = provider.Authenticate(new UserNameSecurityToken("Administrator", "XXXX"));//Supply correct password /// //Administrator changes Jimmy's log on name. /// bool updated = ZentityUserManager.UpdateLogOnName(adminToken, "Jimmy", "Jimmy12"); /// if (updated) /// { /// Console.WriteLine("Updated log on name successfully"); /// } /// else /// { /// Console.WriteLine("Could not update log on name."); /// } /// </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); } }
/// <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("JohnDE", "john", "john@123"); /// 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."); /// } /// } /// 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); } }