Exception thrown in SslStream.AuthenticateAsServer function

Asked 2 years ago, Updated 2 years ago, 52 views

In C#(VisualStudio 2017 Community.Net Framework 4.7.1), we create something like a simple web server to access from a web browser.
I didn't think about security because it was a local system, but now that I have to consider using TLS, I decided to start with something that I can communicate with my browser simply.

So MSDN Sample

https://docs.microsoft.com/ja-jp/dotnet/api/system.net.security.sslstream?view=net-5.0

I made it by referring to
from the browser (Edge, Chrome, Firefox) "https://xxx.xxx.xxx.xxx:8080" (where xxx is the IP address of the PC)

in the sslStream.AuthenticateAsServer function when accessing the . An exception error AuthenticationException will occur.
"The call to SSPI failed.See Internal Exceptions." in
The internal exception is "Local security authority inaccessible".
The port number 8080 was the same when I changed it to 443, etc.

The certificate was created using openssl, and Common Name tried a few proper names, IP addresses to access, and the condition remained the same.

Any information is fine.
Can someone help me?

static X509 Certificate serverCertificate=null;


    private void button1_Click(object sender, EventArgse)
    {

            string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
            path+=@"\server.pfx";

            X509Certificate.CreateFromCertFile(path);
            serverCertificate=new X509Certificate2(path, "password");

            TcpListener listener = new TcpListener (IPAddress.Any, 8080); 
            listener.Start();

            while(true)
            {
                    TcpClient client=listner.AcceptTcpClient();
                    ProcessClient (client);
            }
    }

    void ProcessClient (TcpClient client)
    {
            SslStream sslStream=new SslStream(client.GetStream(), false);

            try
            {
                    sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls12, true);
                    sslStream.ReadTimeout=5000;
                    sslStream.WriteTimeout=5000;
                    string messageData=ReadMessage(sslStream);

                    byte [] message=Encoding.UTF8.GetBytes("Hello from the server.<EOF>");
                    sslStream.Write(message);
            }
            catch(AuthenticationException ex)
            {
                    string errmsg=ex.Message;
                    if(ex.InnerException!=null)
                    {
                            string errmsg2 = ex.InnerException.Message;
                    }
                    sslStream.Close();
                    client.Close();
            }
            finally
            {
                    sslStream.Close();
                    client.Close();
            }
    }

c#

2022-09-29 20:26

1 Answers

Is it helpful that says "I cannot access the local security agency"?


2022-09-29 20:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.