Can I use Serilog Enrichers to get Machine / Hostname Name? - asp.net-core

I have been searching on how I can get client hostname/computer name using ASP.Net Core MVC using .net 3.1 .
There are threads here on SO but most of them are not working on intranet apps (clients used VPN to connect to network).
I've seen some suggestion on some thread to use Serilog Enrichment.
My question is how can I use this one? Can I really get the Machine Name (value passed on the application to db) using this plugin?

You should be able to. Try this configuration on:
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [ "WithMachineName", "WithClientIp" ]
You'd need the nuget package Serilog.Enrichers.ClientInfo
UPDATE:
You can also write the logs directly to the database using one of the database sinks. For eg, to write to a MSSQL database, you could use the sink here: Serilog.Sinks.MSSqlServer
This gives you control over what properties and you want to log including Client Info

Related

Redis Connection with Cache is failing

Goal:
Enable to use Redis Cache in a asp.net core sample project from this webpage.
(https://dotnetcoretutorials.com/2017/01/06/using-redis-cache-net-core/)
Problem:
When I follow the instruction and use the source code I get a error message saying
"StackExchange.Redis.RedisConnectionException: 'It was not possible to connect to the redis server(s); to create a disconnected multiplexer, disable AbortOnConnectFail. SocketFailure on PING"
Q1.
What part of the website or the source code am I missing?
Q2.
Do you need an account at Redis in order use an unique IP number that should be applied at "option.Configuration = "127.0.0.1";"?
Info:
Using VS 2017 asp.net core mvc 2.2
Thank you!

ASP.NET Core Data Protection Key stored to ContentRootPath does not work on different machines

I have Setup a Database for developing that is available in the local network.
I implemented the Dataprotection Api to encrypt some of the sensitive information of my models(Entity Framework), before saving it to the database.
In Startup I configured it like this:
var keysfolder = Path.Combine(Environment.ContentRootPath, "Keys");
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(keysfolder));
The Key is in the folder,not protected and shared in the repository because it is only for Test Data.
I can access the data in my app on 2 different Linux machines but on one Windows PC I get a Invalid Payload exception.
They share the same commit and use the same purpose strings.
So I must have failed to understand it. I thought that I can backup the keys and the database in production and redeploy, if necesarry on a different machine without loosing the data.
Can anybody explain why I canĀ“t use the key on the Windows PC?
I have a solution to this problem.
I found the answer here: https://techcommunity.microsoft.com/t5/iis-support-blog/system-security-cryptography-cryptographicexception-the-payload/ba-p/1919096
You have to call SetApplicationName when registering DataProtection:
services.AddDataProtection()
.SetApplicationName("MyApp")
.PersistKeysToFileSystem(new DirectoryInfo(keysfolder))

Publish .Net Core app with Entity Framework to IIS

I am trying to published my .Net Core app to IIS that uses Entity Framework. I can publish it fine but no database is included in the publish wizard.
I have followed different tutorials including this:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.0
However none tell you how to deploy your database or what needs to be done if using entity framework. I'm relatively new to published and hosting web apps so I'm not sure what to do.
At the moment my frontend of the web app loads on IIS but when I go to login it brings up a 500 error. I have included my connection string and added a user and gve correct permissions under SSMS.
It came to my attention when publishing it shows "no databases found int he project".
Would this effect me not being able to access the database and bringing up the 500 error when logging in and how do i fix this.
No databases found in the project
How did you created your database locally in the first place? Did you manually ran the database update command? I would suggest you add to your startup.cs file a code to ensure your database is created and any missing migrations were applied, you can achieve this within your Configure method:
using (var scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
scope.ServiceProvider.GetRequiredService<DbContext>().Database.Migrate();
}
Or just EnsureCreated() instead of Migrate() if you don't to apply missing migrations on future loads.
It seems that you need to change your ConnectionString configuration in appsettings.json from the format
"Data": {
"DefaultConnection": {
"ConnectionString": "xxxxxx"
}
}
to:
"ConnectionStrings": {
"DefaultConnection": "xxxxxx"
}
And your startup with
services.AddDbContext<ApplicationDbContext>(opt => opt.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));

ASP NET MVC 6 Web App hosted on Azure not running

I have a MVC 6 application which is hosted in Azure as web app. It is running fine on local by iisexpress but when I hosted it in Azure using publish option in Visual Studio, it is not working. It is just waiting to load website. After 4-5 minutes wait, it displays nothing.
I also tested it with static file. I uploaded a static html file in wwwroot folder but it is also not working.
I am not getting how can I see the actual error. Anybody can help me?
Since you've provided almost no information I gonna have to guess, since I had a similar error this weekend when deploying an ASP.NET Core 1.0 WebApp to an Azure App Service.
The issue I was facing was, that the main application wouldn't start. After a couple of hours trying to figure it out I got an error message with the method mentioned in the comments (running web.cmd manually) which returned an Microsoft.AspNet.Server.Kestrel.Networking.UvException: Error -4089 EAFNOSUPPORT address family not supported exception.
I couldn't find anything directly related to it, but was a hint about an IPv6 problem. A few other SO posts hinted that setting http://*:12345 as port binds to both, IPv4 and IPv6. So after I changed the url to http://0.0.0.0:12345 in the hosting.json, it started without issues.
I filled an issue on Kestrel GitHub and this seems to be a bug in Kestrel.
Edit: In response to the comment:
I used to have
{
"server": "Microsoft.AspNet.Server.Kestrel",
"server.urls": "http://*:8081"
}
and changed it to
{
"server": "Microsoft.AspNet.Server.Kestrel",
"server.urls": "http://0.0.0.0:8081"
}
The difference seems to be that http://*:8081 also binds to both IPv4 and IPv6 on the same port, whereas http://0.0.0.0:8081 only binds on IPv4 and not on IPv6.

setting "server name" when using kestrel with dotnet core

I am writing an app using asp.net mvc that right now runs against microsoft's kestrel server. It works fine, and I pass values to it at startup using a hosting.json file defined as follows;
{
"server": "Microsoft.AspNet.Server.Kestrel",
"server.urls": "http://localhost:5000;https://localhost:5001"
}
Is there any way to give the "server" a name, so that it isn't always reporting as localhost or undefined all the time?
Please replace localhost with *. This will give the below benefits:
If you give localhost, you can access the application only through http://localhost:5000 and not using IP [http://192.168.1.2:5000, an example]. By using *, you can access through the IP.
You can connect to the server from other PCs or Mobile Devices.
A few days back, I had the same requirements as yours and got the solution from: docs.asp.net. [The Comments section is hidden by default. Please click Show Comments and search for localhost.]
Tip 1: To view the application from a mobile device, please check: Viewing localhost website from mobile device.
Tip 2: My question and answer has been posted here: Compiling an ASP.NET 5 Web Application and generating DLL files.