public async Task <ActionResult> Authorize(long chatId, IFormCollection form) { var accessToken = form .FirstOrDefault(param => param.Key == "access_token") .Value .FirstOrDefault(); var internalChatId = form .FirstOrDefault(param => param.Key == "state") .Value .FirstOrDefault(); Guid secureKey; if (string.IsNullOrEmpty(accessToken) || !Guid.TryParse(internalChatId, out secureKey)) { return(BadRequest()); } try { await authorizationService.SetTokenAsync(chatId, secureKey, accessToken); } catch (DomainException) { return(BadRequest()); } crmService.ForgetClient(chatId); var user = await crmService.GetUserAsync(chatId); var internalUserId = await userService.UpsertAsync(new User() { CrmUserId = user.Id, FirstName = user.FirstName, LastName = user.LastName, TimeZoneCode = user.TimeZoneCode, BranchId = user.BranchId }); await telegramChatService.SetUserAsync(chatId, internalUserId); await telegramBot.NotifySuccessfulConnectionAsync(chatId); return(Ok()); }