why certificates are null using .NET Core 2 but it works just fine with .NET Framework 4.6.2? - httpwebrequest

I have been doing some test migrating .NET Framework 4.6.2 apps to .NET Core 2. I noticed that this particular app, a monitoring http is not working fine with Net Core 2. Can you please help me to verify what is happening?
static void Main(string[] args)
{
try
{
HttpWebRequest myhttpWebReqest = (HttpWebRequest)WebRequest.Create("https://www.google.com.mx/");
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myhttpWebReqest.GetResponse();
timer.Stop();
TimeSpan timeSpan = timer.Elapsed;
Console.WriteLine(timeSpan.ToString());
Console.WriteLine();
Console.WriteLine(myHttpWebResponse.StatusCode);
Console.WriteLine((int)myHttpWebResponse.StatusCode);
Console.WriteLine();
Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.GetEffectiveDateString());
Console.WriteLine();
Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.GetExpirationDateString());
Console.WriteLine();
Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.Issuer);
Console.WriteLine();
Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.Subject);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if(ex.InnerException !=null)
{
Console.WriteLine(ex.InnerException);
}
}
Console.ReadLine();
}
}
in the .NET Framework 4.6.2 i see the certificate data, in the .NET Core 2 i see myhttpWebReqest.ServicePoint.Certificate null ... do you know why?

See discussion of this here: https://github.com/dotnet/corefx/issues/36979
ServicePointManager and ServicePoint classes are no-op on .NET Core. But you can do a similar thing with HttpClient. HttpClient is the more modern and preferred HTTP API in .NET Framework and .NET Core.
using System;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace NetCoreConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = CustomCallback;
var client = new HttpClient(handler);
HttpResponseMessage response = client.GetAsync("https://www.google.com.mx/").GetAwaiter().GetResult();
Console.WriteLine(response.StatusCode);
Console.WriteLine((int)response.StatusCode);
}
private static bool CustomCallback(HttpRequestMessage arg1, X509Certificate2 arg2, X509Chain arg3, SslPolicyErrors arg4)
{
Console.WriteLine(arg2.GetEffectiveDateString());
Console.WriteLine(arg2.GetExpirationDateString());
Console.WriteLine(arg2.Issuer);
Console.WriteLine(arg2.Subject);
return arg4 == SslPolicyErrors.None;
}
}
}

Related

How to have a Self Hosting signalR server running as as NetCore Console App

I would like to create a SignalR Self hosting Server within a console app using .NetCore.
I am completely new to web development and .Net Core but would like to use SignalR as a real-time web based protocol. No web page is required, and so I would like to have a console app.
I have successfully tested the .Net Framework example below and would like to replicate this using .Net Core 3.1, so that it can run on Linux. However I cannot find any suitable examples.
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
namespace SignalRSelfHost
{
class Program
{
static void Main(string[] args)
{
// This will *ONLY* bind to localhost, if you want to bind to all addresses
// use http://*:8080 to bind to all addresses.
// See http://msdn.microsoft.com/library/system.net.httplistener.aspx
// for more information.
string url = "http://localhost:8088";
using (WebApp.Start<Startup>(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
Clients.All.addMessage(name, "World");
}
}
}
In an attempt to use Owin to create a server console app I have the following code and this compiles, however complains about no server service being registered when I run the program. Could someone please advise what to add to have a web server without web page? The example I copied specified UseKestrel() but I think this is for a web page, so I think I need something else.
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace OwinConsole
{
public class Startup
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app)
{
app.UseOwin(pipeline =>
{
pipeline(next => OwinHello);
});
}
public Task OwinHello(IDictionary<string, object> environment)
{
string responseText = "Hello World via OWIN";
byte[] responseBytes = Encoding.UTF8.GetBytes(responseText);
// OWIN Environment Keys: http://owin.org/spec/spec/owin-1.0.0.html
var responseStream = (Stream)environment["owin.ResponseBody"];
var responseHeaders = (IDictionary<string, string[]>)environment["owin.ResponseHeaders"];
responseHeaders["Content-Length"] = new string[] { responseBytes.Length.ToString(CultureInfo.InvariantCulture) };
responseHeaders["Content-Type"] = new string[] { "text/plain" };
return responseStream.WriteAsync(responseBytes, 0, responseBytes.Length);
}
}
}
using System.IO;
using Microsoft.AspNetCore.Hosting;
namespace OwinConsole
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
Thanks.
For those who want to achive this in .NET 6:
To create a simple server as a console application, you have to create a new empty ASP.NET Core project. In .NET 6 you don't need the 'startup.cs' anymore. You just need to change a few things in 'Program.cs' to configure SignalR.
Program.cs
var builder = WebApplication.CreateBuilder(args);
//builder.Services.AddRazorPages();
builder.Services.AddSignalR();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
//app.MapRazorPages();
app.MapHub<ChatHub>("/chatHub");
app.Run();
Add a Hub
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
Console.WriteLine("Received message, sending back echo");
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
Client (console application)
For example:
using Microsoft.AspNetCore.SignalR.Client;
namespace Client
{
public class Program
{
private static HubConnection _connection;
public static async Task Main(string[] args)
{
_connection = new HubConnectionBuilder()
.WithUrl("https://localhost:7116/chatHub")
.Build();
_connection.Closed += async (error) =>
{
await Task.Delay(new Random().Next(0, 5) * 1000);
await _connection.StartAsync();
};
await ConnectAsync();
bool stop = false;
while (!stop)
{
Console.WriteLine("Press any key to send message to server and receive echo");
Console.ReadKey();
Send("testuser", "msg");
Console.WriteLine("Press q to quit or anything else to resume");
var key = Console.ReadLine();
if (key == "q") stop = true;
}
}
private static async Task ConnectAsync()
{
_connection.On<string, string>("ReceiveMessage", (user, message) =>
{
Console.WriteLine("Received message");
Console.WriteLine($"user: {user}");
Console.WriteLine($"message: {message}");
});
try
{
await _connection.StartAsync();
}
catch(Exception e)
{
Console.WriteLine("Exception: {0}", e);
}
}
private static async void Send(string user, string msg)
{
try
{
await _connection.InvokeAsync("SendMessage", user, msg);
}
catch (Exception e)
{
Console.WriteLine($"Exception: {e}");
}
}
}
}
The client will connect to the server, after that in a loop you can send a message to the server by pressing any key and the server will send you the same message back.
In 'launchSettings.json' (Server) you can find the applicaitonUrl
As mentioned by Noah, my solution was based on
[https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-5.0&tabs=visual-studio]
But instead was built as a console app referencing Microsoft.AspNetCore.App (2.2.8).
ChatHub
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace SignalRServer
{
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
}
Startup
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace SignalRServer
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//services.AddRazorPages();
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
//endpoints.MapRazorPages();
endpoints.MapHub<ChatHub>("/chatHub");
});
}
}
}
Program
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace SignalRServer
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls("http://localhost:2803");
});
}
}

.NET Core Middleware is not invoked

Hello i have the following problem:
I am trying to send an object from a Console Application to a ASP NET Core Server via websockets.
On the Console application i serialize an object with NewtonSoft Jsonand i send it over the Socket to the .NET Core server.
On the Server i have created a tiny middleware.The Invoke method just does not get called.I keep two instances of visual studio open and nothing happens on the server side,until i close the connection on the client side and this error pops in on the server side - terminal :
1 Server
1.1 Program
class Program
{
static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel()
.UseSockets()
.UseUrls("http://localhost:8200")
.Build();
}
1.2 Startup class
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<WebSocketService>(x=>new WebSocketService());
}
public void Configure(IApplicationBuilder builder)
{
builder.UseWebSockets();
builder.UseMiddleware<SocketMiddleware>();
}
}
1.3 Middleware
class SocketMiddleware
{
WebSocketService handler;
public SocketMiddleware(WebSocketService _handler,RequestDelegate del)
{
this.handler = _handler;
this.next = del;
}
RequestDelegate next;
public async Task Invoke(HttpContext context)
{
if (!context.WebSockets.IsWebSocketRequest)
{
await this.next(context);
return;
}
await this.handler.AddClientAsync(context.WebSockets);
}
}
Note: I will not post code of the service that the middleware uses since it is not relevant in this case.The invoke does not get called.
2 Client
The client just serializes a POCO class Player and sends it over the socket.It then waits for the server to respond with a list of Player.
static async Task Main(string[] args)
{
Player updatePlayer = new Player(100);
List<Player> players;
Memory<byte> buffer = new byte[2000];
ReadOnlyMemory<byte> toSend;
ReadOnlyMemory<byte> actualReceived;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8200);
Random rand = new Random();
await socket.ConnectAsync(iPEndPoint);
using(NetworkStream stream =new NetworkStream(socket))
{
while (true)
{
updatePlayer.Lat = rand.Next(10, 100);
updatePlayer.Lng = rand.Next(10, 100);
string data=JsonConvert.SerializeObject(updatePlayer);
toSend = Encoding.UTF8.GetBytes(data);
await stream.WriteAsync(toSend);
int received=await stream.ReadAsync(buffer);
actualReceived = buffer.Slice(0, received);
string msg = Encoding.UTF8.GetString(actualReceived.ToArray());
players = JsonConvert.DeserializeObject<List<Player>>(msg);
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
break;
}
}
}
}
P.S Could it be a problem that the server uses websockets and on the client i use the socket class?I figure as long as the destination address and port are okay..its just bytes.
In the Server.cs the Invoke method has a parameter of type WebSocketManager.We can accept a WebSocket connection but not a raw Socket.Therefore in the Client.cs i replaced the raw Socket with a WebSocket implementation.

WCF REST Self-Hosted 400 Bad Request

I'm having a problem with a self-host WCF REST service.
When I try to issue a GET via browser or Fiddler, I get a 400 Bad Request. Tracing is reporting an inner exception of XmlException "The body of the message cannot be read because it is empty."
I don't have any configuration in app.config (do I need any?). I have tried changing WebServiceHost to ServiceHost, and WSDL is returned, but the operations still return 400.
What am I missing here?
// Add Reference to System.ServiceModel and System.ServiceModel.Web
using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
namespace WCFRESTTest
{
class Program
{
static void Main(string[] args)
{
var baseAddress = new Uri("http://localhost:8000/");
var host = new WebServiceHost(typeof(RestService), baseAddress);
try
{
host.AddServiceEndpoint(typeof(IRestService), new WSHttpBinding(), "RestService");
var smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
host.Description.Behaviors.Add(smb);
host.Open();
Console.WriteLine("Service Running. Press any key to stop.");
Console.ReadKey();
}
catch(CommunicationException ce)
{
host.Abort();
throw;
}
}
}
[ServiceContract]
public interface IRestService
{
[OperationContract]
[WebGet(UriTemplate = "Test")]
bool Test();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class RestService : IRestService
{
public bool Test()
{
Debug.WriteLine("Test Called.");
return true;
}
}
}
When you use the WebServiceHost, you typically don't need to add a service endpoint - it will add one with all behaviors required to make it a "Web HTTP" (a.k.a. REST) endpoint (i.e., an endpoint which doesn't use SOAP and you can easily consume with a tool such as Fiddler, which seems to be what you want). Also, Web HTTP endpoints aren't exposed in the WSDL, so you don't need to add the ServiceMetadataBehavior either.
Now for why it doesn't work - sending a GET request to http://localhost:8000/Test should work - and in the code below it does. Try running this code, and sending the request you were sending before with Fiddler, to see the difference. That should point out what the issue you have.
public class StackOverflow_15705744
{
[ServiceContract]
public interface IRestService
{
[OperationContract]
[WebGet(UriTemplate = "Test")]
bool Test();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class RestService : IRestService
{
public bool Test()
{
Debug.WriteLine("Test Called.");
return true;
}
}
public static void Test()
{
var baseAddress = new Uri("http://localhost:8000/");
var host = new WebServiceHost(typeof(RestService), baseAddress);
// host.AddServiceEndpoint(typeof(IRestService), new WSHttpBinding(), "RestService");
// var smb = new ServiceMetadataBehavior();
// smb.HttpGetEnabled = true;
// host.Description.Behaviors.Add(smb);
host.Open();
WebClient c = new WebClient();
Console.WriteLine(c.DownloadString(baseAddress.ToString().TrimEnd('/') + "/Test"));
Console.WriteLine("Service Running. Press any key to stop.");
Console.ReadKey();
}
}

WCF application don't working in my home network

i create a small wcf apps when i am testing this apps in my machine then it is working but when i run the wcf server end on another pc at my home in same network then i am getting error
A remote side security requirement was not fulfilled during authentication.Try increasing the ProtectionLevel and/or ImpersonationLevel.
both the pc at my home in same work group and they can access each other. i try to find out the answer but people say this is firewall issue. so i disable firewall at both the pc but still getting the problem. here is my sample code. please guide me how can i run this wcf apps in two pc at my home network. thanks
Service end
namespace WCFSample
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string MyName(string name);
}
}
namespace WCFSample
{
public class Service1 : IService1
{
public string MyName(string name)
{
return string.Format("My Name... {0}", name);
}
}
}
namespace ConsoleApplication1
{
class Program
{
static ServiceHost customerHost = null;
static void Main(string[] args)
{
try
{
HostCustomerService();
Console.WriteLine();
Console.WriteLine("Press any key to stop the services.");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
customerHost.Close();
}
}
private static void HostCustomerService()
{
customerHost = new ServiceHost(typeof
(Service1));
ServiceEndpoint tcpEndpoint = customerHost.AddServiceEndpoint(
typeof(IService1), new NetTcpBinding(),
"net.tcp://192.168.1.103:9020/Service1");
customerHost.Open();
Console.WriteLine("{0} {1}", tcpEndpoint.Address, tcpEndpoint.Name);
Console.WriteLine();
}
}
}
client end
namespace Client1
{
class Program
{
static void Main(string[] args)
{
IService1 channel = null;
var endPoint = new EndpointAddress(
"net.tcp://192.168.1.103:9020/Service1");
channel = ChannelFactory<IService1>.CreateChannel(new NetTcpBinding(), endPoint);
Console.WriteLine("Enter Name");
string line = Console.ReadLine();
Console.WriteLine(channel.MyName(line));
Console.ReadKey();
}
}
}
I think by default, NetTcpBinding requires a secure channel.
When you create your binding (on client and server), instead of:
new NetTcpBinding()
Try:
new NetTcpBinding(SecurityMode.None)

WCF over Named Pipes throws exception in .NET4, works in 3.5

I'm trying to run this example derived from this blog entry on
WCF Tutorial - Basic Interprocess Communication
If I run the server code in .NET4, it throws the following exception:
First-chance exception at 0x754cd36f (KernelBase.dll) in TestConsole.exe: 0xE0564552: 0xe0564552.
If I run the server code in .NET3.5, it works just fine. Client code is compiled against .NET4 in both tests. My server code is as follows:
[ServiceContract]
public interface IStringReverser
{
[OperationContract]
string ReverseString(string value);
}
public class StringReverser : IStringReverser
{
public string ReverseString(string value)
{
char[] retVal = value.ToCharArray();
int idx = 0;
for (int i = value.Length - 1; i >= 0; i--)
retVal[idx++] = value[i];
return new string(retVal);
}
}
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(StringReverser), new Uri[] { new Uri("net.pipe://localhost") }))
{
host.AddServiceEndpoint(typeof(IStringReverser), new NetNamedPipeBinding(), "PipeReverse");
host.Open();
Console.WriteLine("Service is available. Press <ENTER> to exit.");
Console.ReadLine();
host.Close();
}
}
}
My client code is as follows:
[ServiceContract]
public interface IStringReverser
{
[OperationContract]
string ReverseString(string value);
}
class Program
{
static void Main(string[] args)
{
ChannelFactory<IStringReverser> pipeFactory =
new ChannelFactory<IStringReverser>(
new NetNamedPipeBinding(),
new EndpointAddress(
"net.pipe://localhost/PipeReverse"));
IStringReverser pipeProxy = pipeFactory.CreateChannel();
while (true)
{
string str = Console.ReadLine();
Console.WriteLine("pipe: " +
pipeProxy.ReverseString(str));
}
}
}
Why on earth is this failing on .NET4? Seems like a pretty basic example. I did do a clean/build between each run. Here is a snapshot of the actual stacktrace:
It turns out I had the "throw" checked in Debug -> Exceptions -> C++ Exceptions while in Visual Studio. If I don't throw the exception, but let it be handled, everything works fine.