/// <summary> /// Executes an authentication request to the API using the parameters. If successful /// the token is set to the Token property. /// </summary> /// <param name="username">Username part of the credentials to authenticate with</param> /// <param name="password">Password part of the credentials to authenticate with</param> /// <param name="messages">Outputs the messages returned from the API</param> /// <returns>A value indicating whether the authentication is successful</returns> public bool TryAuthenticate(string username, string password, out IEnumerable<string> messages) { if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["TimeLogProjectRawTokenHash"])) { this.token = new SecurityToken { Expires = DateTime.ParseExact(ConfigurationManager.AppSettings["TimeLogProjectRawTokenExpires"], "yyyyMMddHHmmssK", new CultureInfo("da-DK")), Hash = ConfigurationManager.AppSettings["TimeLogProjectRawTokenHash"], Initials = ConfigurationManager.AppSettings["TimeLogProjectRawTokenInitials"] }; messages = new List<string>(); return true; } // Reuse the token if we already have it in the cache if (this.cachedTokens.ContainsKey(username)) { this.token = this.cachedTokens[username]; // Check if the token has expired - leave a minute to other code to run if (this.token.Expires > DateTime.Now.AddMinutes(1)) { messages = new List<string>(); return true; } this.cachedTokens.Remove(username); } var tokenResponse = this.SecurityClient.GetToken(username, password); if (tokenResponse.ResponseState == SecurityService.ExecutionStatus.Success && tokenResponse.Return.Any()) { this.token = tokenResponse.Return[0]; this.cachedTokens.Add(username, this.token); messages = new List<string>(); return true; } messages = tokenResponse.Messages.Select(m => string.Concat(m.ErrorCode, " ", m.Message)); return false; }
/// <summary> /// Disposes the handler. /// </summary> public void Dispose() { this.securityClient = null; this.token = null; instance = null; }