public async Task <IHttpActionResult> Post(Guid authenticationId, string documentUnit, short year, int number) { return(await ActionHelper.TryCatchWithLoggerGeneric(async() => { _logger.WriteInfo(new LogMessage($"Token securities receive a request for authenticationId {authenticationId}, documentUnit {documentUnit}, year {year} and number {number}"), LogCategories); if (!DSWAuthorizationServerProvider.ValidAuthenticationList.Any(f => f == authenticationId)) { _logger.WriteWarning(new LogMessage($"AuthenticationId {authenticationId} is not valid"), LogCategories); throw new DSWSecurityException($"AuthenticationId {authenticationId} is not valid", null, DSWExceptionCode.SC_InvalidAccount); } DocumentUnit reference = null; switch (documentUnit) { case "Protocol": { reference = _unitOfWork.Repository <DocumentUnit>().GetByNumbering(year, number, (int)DSWEnvironmentType.Protocol, optimization: true); break; } default: { _logger.WriteWarning(new LogMessage($"AuthenticationId {authenticationId} has no valid documentUnit '{documentUnit}' name"), LogCategories); throw new DSWSecurityException($"AuthenticationId {authenticationId} is not valid", null, DSWExceptionCode.SC_InvalidAccount); } } if (reference == null) { _logger.WriteWarning(new LogMessage($"DocumentUnit {documentUnit} - {year}/{number} not found"), LogCategories); throw new DSWSecurityException($"DocumentUnit {documentUnit} - {year}/{number} not found", null, DSWExceptionCode.SC_InvalidAccount); } Guid currentToken = Guid.NewGuid(); DateTimeOffset creationDate = DateTimeOffset.UtcNow; DateTimeOffset expiryDate = creationDate.AddMilliseconds(30 * 1000); HostIdentify hostIdentify = new HostIdentify(Environment.MachineName, "Public WebAPI"); TokenSecurityModel tokenModel = new TokenSecurityModel() { AuthenticationId = authenticationId, ExpiryDate = expiryDate, Token = currentToken, Host = hostIdentify, DocumentUnitAuhtorized = new DocSuiteWeb.Model.Entities.DocumentUnits.DocumentUnitModel() { Environment = reference.Environment, UniqueId = reference.UniqueId, Year = reference.Year, Number = reference.Number.ToString(), Title = reference.Title } }; string username = "******"; if (WindowsIdentity.GetCurrent() != null) { username = WindowsIdentity.GetCurrent().Name; } IdentityContext identityContext = new IdentityContext(username); EventTokenSecurity eventTokenSecurity = new EventTokenSecurity(Guid.NewGuid(), currentToken, _parameterEnvService.CurrentTenantName, _parameterEnvService.CurrentTenantId, Guid.Empty, identityContext, tokenModel, null); _logger.WriteDebug(new LogMessage($"Generated token {currentToken} by {username} expiry on {expiryDate}"), LogCategories); string topicName = _messageConfiguration.GetConfigurations()[eventTokenSecurity.EventName].TopicName; string dynamicSubscriptionName = currentToken.ToString(); await _topicService.CreateSubscriptionAsync(topicName, dynamicSubscriptionName, currentToken.ToString(), eventTokenSecurity.EventName); ServiceBusMessage message = _cqrsMapper.Map(eventTokenSecurity, new ServiceBusMessage()); ServiceBusMessage response = await _topicService.SendToTopicAsync(message); return Ok(currentToken); }, _logger, LogCategories)); }