简体   繁体   English

c# - 如何在c#中使用“授权”、“承载”、令牌创建指向URL的websocket

[英]How to create a websocket to a URL using "Authorization", "Bearer", token in c#

I need to create a websocket to a particular url(cannot reveal) in c#.我需要在 c# 中为特定的 url(无法显示)创建一个 websocket。 Is there something similar to the WebSocket API in Java, or libwebsockets in C for C#?是否有类似于 Java 中的 WebSocket API 或 C# 中的 libwebsockets 的东西?

I have already tried using WebSocketSharp and ChilKat by following some of the SO answers.我已经按照一些 SO 答案尝试使用 WebSocketSharp 和 ChilKat。 Also tried using the Microsofts WebSocket Namespace.还尝试使用 Microsoft 的 WebSocket 命名空间。 But was always getting the "Not Authorized" error 401. I also tried doing an Http get and tried to add headers to upgrade the socket to Websocket by following the tutorial to create WebSocket servers in the MDN docs, but was only getting a redirected webpage in return.但总是收到“未授权”错误 401。我也尝试执行 Http get 并尝试按照 MDN 文档中创建 WebSocket 服务器的教程添加标头以将套接字升级到 Websocket,但只得到重定向的网页作为回报。 This is the code I used for the HTTP upgrade request.这是我用于 HTTP 升级请求的代码。 I am a beginner to c#.我是 C# 的初学者。

ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                                    | SecurityProtocolType.Tls11
                                                    | SecurityProtocolType.Tls12
                                                    | SecurityProtocolType.Ssl3;
            ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
            {
                return true;
            };
            var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
            http.AllowAutoRedirect = false;
            http.UseDefaultCredentials = true;
            http.PreAuthenticate = true;
            http.Credentials = CredentialCache.DefaultCredentials;
            http.Proxy.Credentials = CredentialCache.DefaultCredentials;
            http.Accept = "application/json";
            http.ContentType = "application/json";
            http.Method = "GET";
            http.Connection = "Open";
            http.Headers.Add("Authorization", "Bearer " + Token);
            //http.Headers.Add("Connection", "Upgrade");
            http.Headers.Add("Upgrade", "websocket");
            http.KeepAlive = true;

What I want to achieve is something like,我想要实现的是类似的东西,

Websocket socket = new Websocket();
socket.addHeader(Authorization, Bearer, Token);
socket.connect(); 

Thanks in Advance for any inputs.提前感谢您的任何投入。

I am using ClientWebSocket to connect to the Zendesk chat streaming API using an OAuth token like this:我正在使用ClientWebSocket使用 OAuth 令牌连接到 Zendesk 聊天流 API,如下所示:

var webSocket = new ClientWebSocket();
webSocket.Options.SetRequestHeader("Authorization", $"Bearer {_oauthToken}");
var cts = new CancellationTokenSource();
await webSocket.ConnectAsync(new Uri(url), cts.Token);

It works for me!这个对我有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM