Esempio n. 1
0
        public void Authenticate()
        {
            string responseData = null;
            //			var urlRegex = new Regex(@"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$");
            var urlRegex = new Regex(@"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)");
            var match    = urlRegex.Match(_credentials.TokenUrl);

            if (match.Success)
            {
                _client.BaseAddress    = match.Value;
                _client.EndpointMethod = _credentials.TokenUrl.Replace(_client.BaseAddress, string.Empty);
            }

            _client.ContentType = "application/x-www-form-urlencoded";

            responseData = _client.Post($"grant_type=client_credentials&client_id={_credentials.ConsumerKey}&client_secret={_credentials.ConsumerSecret}&scope={_credentials.Scope}");

            if (responseData != null)
            {
                var tokenResponse = _serializationUtility.Deserialize <TokenResponse>(responseData);

                if (!string.IsNullOrEmpty(tokenResponse.error))
                {
                    throw new Exception($"Authentication Error: {tokenResponse.error} - {tokenResponse.error_description}");
                }

                _credentials.AccessToken = tokenResponse.access_token;
            }
        }
        public void Authenticate()
        {
            string responseData = null;
            //			var urlRegex = new Regex(@"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$");
            var urlRegex = new Regex(@"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)");
            var match    = urlRegex.Match(_credentials.TokenUrl);

            if (match.Success)
            {
                _client.BaseAddress    = match.Value;
                _client.EndpointMethod = _credentials.TokenUrl.Replace(_client.BaseAddress, string.Empty);
            }

            // https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin/context

            _client.AddParameter("response_type", "code");
            _client.AddParameter("client_id", _credentials.ConsumerKey);
            _client.AddParameter("redirect_uri", _credentials.CallbackUri);
            _client.AddParameter("state", _credentials.State);
            _client.AddParameter("scope", _credentials.Scope);

            responseData = _client.Get();

            if (responseData != null)
            {
                var tokenResponse = _serializationUtility.Deserialize <TokenResponse>(responseData);

                if (!string.IsNullOrEmpty(tokenResponse.error))
                {
                    throw new Exception($"Authentication Error: {tokenResponse.error} - {tokenResponse.error_description}");
                }

                _credentials.AccessToken = tokenResponse.access_token;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// This message is registered on the subscription client and is called in a separate thread.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        private async Task OnMessageCallback(BrokeredMessage brokeredMessage, CancellationToken token)
        {
            if (brokeredMessage != null)
            {
                IMessage message = null;

                try
                {
                    var data = Encoding.UTF8.GetString(brokeredMessage.Body);

                    message = new Message
                    {
                        Payload  = (brokeredMessage.ContentType == null) ? data : (object)_serializationUtility.Deserialize <TType>(data),
                        Metadata = new MessageMetadata
                        {
                            MessageId = brokeredMessage.MessageId,
                            DeadLetterErrorDescription = brokeredMessage.UserProperties["DeadLetterErrorDescription"].ToString(),
                            DeadLetterReason           = brokeredMessage.UserProperties["DeadLetterReason"].ToString()
                        },
                        Success = !token.IsCancellationRequested
                    };

                    OnMessageCallback(message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                // If this subscription is peek only then don't give up any of the peek locks so the next message will be processed.

                if (!_peekOnly)
                {
                    if (message?.Success == true)
                    {
                        await _subscriptionClient.CompleteAsync(brokeredMessage.SystemProperties.LockToken);
                    }
                    else
                    {
                        await _subscriptionClient.AbandonAsync(brokeredMessage.SystemProperties.LockToken);
                    }
                }

                // TODO: Add dead letter?
            }
        }