async/await: Why don’t use Task.Wait()?

In short explained why you should never use Task.Wait() to wait for an asynchronous task to complete from a synchronous context.

The Wait() method always returns an AggregateException if an error occurs within the task execution.

It would be hard to handle the real exception which is wrapped within the AggregateException.

Use GetAwaiter().GetResult() instead. It will return the real exception.

Challenge authentication using a custom middleware.

Sometimes you write an ASP.NET Core application without using MVC. For example an microservice or an application serving static files.

Anyhow you might want to secure your application using authentication. I was facing the problem, how I could challenge authentication when I’m not using MVC.

The solution is writing a custom middleware that challenges authentication.

Sending all e-mails in development environment to a local folder

In the app.config/web.config you can use the configuration section <system.net> <mailSettings> <smtp> to configure how to deliver e-mails which are sent using the MailMessage and SmtpClient classes in your program.

In development environment you usually don’t want the e-mails to be delivered. Today I’ve found a awesome configuration option, which enables that all outgoing e-mails go to an local folder or file share as eml files.

app.config / web.config

Sending an e-mail with .NET

Local pickup folder

How to get the Correlation ID of the current request in SharePoint

SharePoint generates for each request a random GUID which correlates all log messages in the ULS log which are printed during the current request. The Correlation ID is also printed to error pages. If you want more information about an error page you can use the Correlation ID to find the right log messages in the ULS log.

Recently I wanted to get the current request’s Correlation ID to share it with an external webservice to have the same correlation ID in all depending systems.

You can add following code to your ASP page to get the Correlation ID.