Oyatel / CometD.NET

CometD.NET is a C# client library for the Bayeux protocol
43 stars 34 forks source link

HTTPS?? #16

Open naikvi opened 12 years ago

naikvi commented 12 years ago

Does this library work for HTTPS for secure communication? Or does it only support HTTP?

kernelfreak commented 8 years ago

I am also looking forward for https support. As I am not able to handshake currently with https based server.

ghost commented 4 years ago

Any update on this issue, as I am also not able to handshake and no error is being thrown even.

ohaucke commented 4 years ago

It does work with HTTPS, it might be that your selected framework version doesn't have the corresponding tls version "activated".

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ghost commented 4 years ago

Thanks for the quick response.

But with this change as well it does not work.

The problem is it does not handshake. It just keeps all status 'connected', 'disconnected' and 'handshook' as false.

Also, it does not throw any errors as well so not getting an idea of what is wrong and where it is wrong.

Is there any way to identify the issue?

Also, is there any tool to test the configuration so that I can ensure that server-side configuration is well.

Jignesh

ohaucke commented 4 years ago

You could try to check the network traffic with tools like fiddler or use "system.diagnostics". Todo so merge the follwoing xml into your app.config file, run your code and check the System.Net.trace.log.

    <system.diagnostics>
      <trace autoflush="true" />
      <sources>
        <source name="System.Net">
          <listeners>
            <add name="MyTraceFile"/>
          </listeners>
        </source>
      </sources>
      <sharedListeners>
        <add name="MyTraceFile" type="System.Diagnostics.TextWriterTraceListener" initializeData="System.Net.trace.log" />
      </sharedListeners>
      <switches>
        <add name="System.Net" value="Verbose" />
      </switches>
    </system.diagnostics>
ghost commented 4 years ago

Thanks,

Fiddler was a trick. I can now see that it gets failed with "failureReason=401::Request requires authentication"

I use below code for authentication


using (var client = new HttpClient())
            {
                var request = new FormUrlEncodedContent(new Dictionary<string, string>
                {
                    {"grant_type", "password"},
                    {"client_id", ClientID},
                    {"client_secret", ClientKey},
                    {"username", UserID},
                    {"password", Password}
                });

                request.Headers.Add("X-PrettyPrint", "1");

                var response = client.PostAsync(BaseURI + "/services/oauth2/token", request).Result;
                jsonResponse = response.Content.ReadAsStringAsync().Result;
            }

It gives with JSON with two values access_token and instance_url. And following is the code for handshake.

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            BayeuxClient bayeuxClient = new BayeuxClient(endpoint, new List<ClientTransport>() { new LongPollingTransport(null) });

            // Handshaking with oauth2
            IDictionary<String, Object> handshakeAuth = new Dictionary<String, Object>();

            handshakeAuth.Add("authType", "oauth2");
            handshakeAuth.Add("oauth_token", access_token);

            bayeuxClient.handshake(handshakeAuth);

            //bayeuxClient.handshake();
            bayeuxClient.waitFor(1000, new List<BayeuxClient.State>() { BayeuxClient.State.CONNECTED });

So, I am sending the access token to the handshake method. Even though it is not working.

Do you find anything wrong here?

Jignesh

ohaucke commented 4 years ago

I'm not sure if handshakeAuth.Add("oauth_token", access_token); is right, in a normal http call it would be the http authentication header Authorization: Bearer your-raccess-token.

Maybe have a look at https://github.com/Oyatel/CometD.NET/pull/19 and set the header this way.

ghost commented 4 years ago

Thanks, Ohaucke

But this also does not work and we get same 401:: Request requires authentication error.

Btw, we got other information from my client and in that, they give Initial Auth Token.

I do not have any idea about as I am not salesforce developer. I am a .net developer for learning management system site and my need is to fetch some data from client's salesforce to use in my system.

I searched on google and found that the initial token will be used to create a client.

Do you have any idea about it? or is there any document where I can know step by step call requirement to create an application which keeps the connection with salesforce system and get real-time data from them.

Jignesh

ohaucke commented 4 years ago

Did you check with fiddler if the Authorization header is present? How did you get the access token?

Basiclly you need to create a Connected App in salesforce with oauth enabled (https://help.salesforce.com/articleView?id=connected_app_create_api_integration.htm) Then you need to do the authorization: user auth -> callback will provide you with the authorization code -> request access token with the authorization code

With the access token and the patch from #19 you create the client with the LongPollingTransport where you added the access token.

ghost commented 4 years ago

Thanks Ohaucke,

We removed nuget reference and downloaded the code from this repository. In that code the header related code was missing which we have added from patch you shared in your previous post

This change has made authorization done. Now I can get following JSON as response.

[{"ext":{"replay":true,"payload.format":true},"minimumVersion":"1.0","clientId":"4d1.........","supportedConnectionTypes":["long-polling"],"channel":"/meta/handshake","id":"0","version":"1.0","successful":true}]

Thanks for all your help.

Jignesh

ghost commented 4 years ago

Hi Ohaucke,

As per my above last post on 27 May, I could make the application which open channels and receive messages well. That application is deployed on the Azure as WebJob.

The problem we are facing right now that, even the application shows running it stops listening to the messages. It does not throw any error. We just need to restart the application to make it work again.

I am clueless about what might be going wrong. Is there any way to write up a code which writes logs on every re-handshaking it does. Or write up code to check if the channel is open or something.

Jignesh

ohaucke commented 4 years ago

Hi @pateljigu210, you could add a listener to "/meta/handshake" like mentioned in #23 or create an extension that raises an event when the connection gets closed https://github.com/Oyatel/CometD.NET/issues/3#issuecomment-389085182

ghost commented 4 years ago

Thanks Ohaucke,

I have implemented listener to "/meta/handshake" and in that listener, I have added code of subscription to other channels.

I believe with this whenever it will do re-handshaking the subscription will be reopened.

But, will it cover the scenario the connection got closed by the server? Or I shall implement that extension as well.

Also when re-handshaking occurs?

Jignesh

ohaucke commented 4 years ago

I'm not sure, i implemented the extension to create a completely new connection.

ghost commented 4 years ago

Ok,

In that case, I shall implement the extension as well.

I saw your code of extension but I do not find anything written for creating a new connection. In OnConnectionError method you just have invoked current object. I am not getting what it will do.

What code I need to write in which method of extension to create new connection.

Jignesh

ohaucke commented 4 years ago

You need to subscribe to the ConnectionError event and when it get raised you know the server refused the connection due to 403::unknown client or what ever you implement.

swagathchandra commented 3 years ago

@ohaucke - Can you please share example of your code as I am facing the same issue.

swagathchandra commented 3 years ago

@pateljigu210 - Were you able to resolve the issue, Can you please share the example of code.

ghost commented 3 years ago

@pateljigu210 - Were you able to resolve the issue, Can you please share the example of code.

----Below is my code for creating client, adding up the listener to the "meta/handshake" and then handshake

BayeuxClient bayeuxClient = new BayeuxClient(endpoint, new[] { transport }); bayeuxClient.getChannel("/meta/handshake").addListener(new Listener(bayeuxClient));

bayeuxClient.handshake(); bayeuxClient.waitFor(1000, new List() { BayeuxClient.State.HANDSHAKING }); bayeuxClient.waitFor(1000, new List() { BayeuxClient.State.CONNECTED });

---- below is my code of listner

public class Listener : IMessageListener { BayeuxClient bayuexClient;

    public Listener(BayeuxClient client)
    {
        bayuexClient = client;
    }

    public Listener()
    {
    }

public void onMessage(IClientSessionChannel channel, IMessage message)
{
    dynamic entity = null;
    entity = JObject.Parse(message.JSON);

        if (entity.channel.ToString() == "/meta/handshake")
        {
                bayuexClient.getChannel("my actual channel").subscribe(new Listener());
        }

    if (entity.channel.ToString() == "my actual channel")
        {
        // actual business logic when receive message on my channel
    }
}

}

swagathchandra commented 3 years ago

@pateljigu210 - thank you for the code. Did your code also take care of subscribing channels on connection error? Do we need to implement extension for “403:client error”

ghost commented 3 years ago

@pateljigu210 - thank you for the code. Did your code also take care of subscribing channels on connection error? Do we need to implement extension for “403:client error”

No, I have not done that

swagathchandra commented 3 years ago

@pateljigu210 - Thank you for update. Does it mean that this approach would also handle connection errors, reconnects etc.

ghost commented 3 years ago

@pateljigu210 - Thank you for update. Does it mean that this approach would also handle connection errors, reconnects etc.

I am not sure, but I believe it does auto-connect on connection errors and while auto-connect re-handshaking happens and on handshaking listener, we have written code to create our actual channel listener.

ghost commented 3 years ago

Hi @pateljigu210, you could add a listener to "/meta/handshake" like mentioned in #23 or create an extension that raises an event when the connection gets closed #3 (comment)

Hi Ohaucke,

I had implemented the listener to the "/meta/handshake" channel and on the message received on this channel, I am creating listener to other actual channels. But now we are seeing that we are receiving the message on "/meta/handshake" channel very frequently like on every 50 seconds.

I am not able to understand why it is doing handshaking so frequently. Can you please guide me on this?

Jignesh

ohaucke commented 3 years ago

@pateljigu210 Sorry i have no idea