小编Ork*_*kar的帖子

SSL认证适用于localhost,但不适用于计算机名称或IP

我们有一个在服务器上运行的Web应用程序,它通过XDomainRequest发布http请求(因为IE9).

有许多客户端计算机具有通过套接字侦听器侦听端口的控制台应用程序.客户端使用IE9浏览器打开Web应用程序,当他们点击链接时,网页会发送以下请求:

" https:// localhost:portNumber/applicationName/doSomething "" https:// computerName:portNumber/applicationName/doSomething "" https:// ipAddress:portNumber/applicationName/doSomething "

第二和第三个请求用于控制其他用户计算机的应用程序.

问题是如果请求带有localhost,则控制台应用程序不会有关于读取传入数据和发送响应的问题.但是,如果请求附带计算机名称或IP地址,则浏览器会显示认证警告,并希望用户单击"继续访问此网站(不推荐)"链接.

我们想通过代码创建三个不同的证书.但即使使用其中三个的sslstream也是可能的,我们无法决定选择真正的认证,因为我们首先进行认证然后接收数据.因此,当我们捕获传入请求时,必须已完成身份验证.

另一种方法是强制套接字侦听器或sslstream表示所有这三个请求,就好像它们是localhost一样.因此,对于每一个,身份验证将作为localhost.但我无法找到实际的方法.

这是代码.我给出了代码,因为可能有一些错误的SslStream用法.

using System;
using System.Net.Sockets;
using System.Net;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
using System.IO;
using System.Net.Security;
using System.Security.Authentication;
using System.Threading;
using System.Text;

namespace StackOverFlowProject
{
    class StackOverFlowSample
    {
        private static ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
        private static X509Certificate _cert = null;

        static void Main(string[] args)
        {
            StackOverFlowSample stackOverFlowSample = new StackOverFlowSample();
            stackOverFlowSample.StartListening();
        }

        private void StartListening()
        {
            GetCertificate();

            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, …
Run Code Online (Sandbox Code Playgroud)

c# sockets ssl https

17
推荐指数
2
解决办法
8119
查看次数

如何将PFX证书文件应用于SslStream套接字侦听器?

我有一个多线程套接字监听器.它侦听一个端口.它从HTTP网站接收数据并根据其请求发送响应.它运作良好.

现在我想用HTTPS网站做同样的事情.所以我把它改为能够通过SslStream而不是socket来读写.

网站所有者给了我一个pfx文件和一个密码.我将它放入我当地认证商店的可信证书列表中.

认证文件已成功从商店中检索.但在调用AuthenticateAsServer方法后,我的处理程序套接字似乎是空的.所以我在ReceiveCallBack方法中看不到任何数据.

如何将pfx证书应用于我的SslStream套接字?

这是我的代码:

using System;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Threading;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.IO;

namespace SslStreamTestApp
{
    public class SslStreamSocketListener
    {
        private static readonly ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
        private readonly string pfxSerialNumber = "12345BLABLABLA";

        X509Certificate _cert = null;

        private void GetCertificate()
        {
            X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);

            store.Open(OpenFlags.ReadOnly);

            X509Certificate2Collection certificateCollection = store.Certificates.Find(X509FindType.FindBySerialNumber, pfxSerialNumber, true);

            if (certificateCollection.Count == 1)
            {
                _cert = certificateCollection[0];
            }
        }

        private void StartListening()
        {
            GetCertificate();

            IPEndPoint …
Run Code Online (Sandbox Code Playgroud)

c# sockets ssl ssl-certificate pfx

13
推荐指数
1
解决办法
1537
查看次数

检查请求是否来自套接字侦听器中的HTTP或HTTPS

我有多线程异步套接字监听器.我想检查请求是否安全.但我想在AcceptCallBack方法中检查不是ReceiveCallBack.

我会这样做,因为我希望我的代码适用于HTTP和HTTPS.如果请求来自HTTPS,我将继续使用经过身份验证的SslStream而不是原始套接字.

这是我的代码:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;

namespace LearnRequestType
{
    class StackOverFlow
    {
        private static readonly ManualResetEvent _manualResetEvent = new ManualResetEvent(false);

        private void StartListening()
        {
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 9002);

            if (localEndPoint != null)
            {
                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                if (listener != null)
                {
                    listener.Bind(localEndPoint);
                    listener.Listen(10);

                    Console.WriteLine("Socket listener is running...");

                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
                }
            }
        }

        private void AcceptCallback(IAsyncResult ar)
        {
            _manualResetEvent.Set();

            Socket listener = (Socket)ar.AsyncState;

            Socket handler = listener.EndAccept(ar); …
Run Code Online (Sandbox Code Playgroud)

c# sockets https http

7
推荐指数
1
解决办法
1519
查看次数

如何为非阻塞异步套接字创建 SslStream

我有一个多线程异步System.Net.Socket,它侦听端口。如果我收到来自 HTTP 的请求,我不会遇到任何问题,但最近我必须向我的应用程序添加 https 支持。

客户给了我一份.arm认证文件。它包含 base-64 编码的 ASCII 数据。文件名是cert.arm,它存储在我的解决方案文件夹的根目录中。

到目前为止,我使用该证书的操作如下:

using System;
using System.Net;
using System.Threading;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace SocketExample
{
    public class StackOverFlow
    {
        public static ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
        private static X509Certificate2 _cert = X509Certificate2("..\\..\\cert.arm");

        static void Main(string[] args)
        {
            StartListening();
        }

        private static void StartListening()
        {
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 9002);

            if (localEndPoint != null)
            {
                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, …
Run Code Online (Sandbox Code Playgroud)

c# sockets

5
推荐指数
1
解决办法
3417
查看次数

是否可以创建一个base64字符串,其中包含多页tiff文件的所有帧?

使用已知的转换方法将多页tiff文件转换为base64字符串似乎只包含一个页面.

我从本地磁盘获取多页tiff文件:

Image multiPageImage = Image.FromFile(fileName);
Run Code Online (Sandbox Code Playgroud)

将其转换为base64字符串:

base64string = ImageToBase64(multiPageImage, ImageFormat.Tiff);

public static string ImageToBase64(Image image, ImageFormat format)
{
    using (MemoryStream ms = new MemoryStream())
    {
        // Convert Image to byte[]
        image.Save(ms, format);
        byte[] imageBytes = ms.ToArray();

        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(imageBytes);

        image.Dispose();

        return base64String;
    }  
}
Run Code Online (Sandbox Code Playgroud)

然后将base64转换为图像并将其保存在本地磁盘上以控制结果:

public static Image ConvertBase64ToImage(string base64string)
{
    byte[] bytes = Convert.FromBase64String(base64string);

    Image image;

    using (MemoryStream ms = new MemoryStream(bytes))
    {
        image = Image.FromStream(ms);

        image.Save(@"C:\newTiff.tiff", ImageFormat.Tiff);
    }

    return image; …
Run Code Online (Sandbox Code Playgroud)

.net c# base64 file

4
推荐指数
1
解决办法
2953
查看次数

标签 统计

c# ×5

sockets ×4

https ×2

ssl ×2

.net ×1

base64 ×1

file ×1

http ×1

pfx ×1

ssl-certificate ×1