Friday, March 16, 2018

Change Windows credentials when connecting to a database using Windows Authentication

Image you have a two accounts in Active Directory. You have one that you log into Windows (call it BasicUser1 for example). The other is one that you use for development and is the account (call it DevUser1 for example) that you need to use to access a MS SQL Server database using Windows Authentication.

Now when running SSMS (SQL Server Management Studio), LINQPad, Visual Studio, etc and trying to connect to a database that requires Windows Authentication (using DevUser1), but you are logged into Windows as BasicUser1 which doesn't have permissions to the database.

The problem is how do we impersonate DevUser1 when connecting to the database using Windows Authentication. The answer is actually pretty simple and seamless once configured.

The answer is the Credential Manager built into Windows. You can find it in the start menu, but you can also run it directly using:

control /name Microsoft.CredentialManager

It looks something like this:

Click the Add a Windows credential link.

Enter the fully qualified server name, etc AND the port for the SQL Server database (the default is 1433). The username should be in the format domain\username. The password is the password for the specified user.

Now when you connect to the database with Windows Authentication Windows will automatically pass the credentials specified in the Credential Manager, not the ones you are currently logged into Windows as.

Friday, February 23, 2018

Good command line or programmatic image manipulation app

Check out ImageMagick to programmatically or from the command prompt manipulate images. It supports a ton of formats. It has support from .NET and Java, etc.

A very good tool to have in your toolbox.

Get Proxy settings when you don't have access to see them

Open a command prompt and enter the following command

netsh winhttp import proxy source = ie

You should get output like:

Current WinHTTP proxy settings:

    Proxy Server(s) :  some.corp-proxy:8080
    Bypass List     :  some.corp-servers...

Friday, February 2, 2018

nslookup like functionality from Powershell

Below is a Powershell script that takes a list of aliases that you want to lookup. For each item in the list it will output a line. Each line is tab separated and composed of the alias, the host name, and the ip address (first one if there are multiples). The output can be copy and pasted into Excel easily. Any errors will be shown in Red.

$aliases = @(

foreach ($alias in $aliases)
        $entry = [System.Net.DNS]::GetHostEntry($alias)
        $tab = [char]9
        $hostname = $entry.HostName
        $ipAddress = $entry.AddressList[0].IPAddressToString
        Write-Host "$alias$tab$hostname$tab$ipAddress"
        Write-Host "$alias could not be processed" -ForegroundColor Red

There are actually Powershell packages that implement nslookup, but they require something be installed, imports, dependencies, etc. The only dependency to run this is that C# be installed and System.Net.DNS be available.

Example output is:

Tuesday, January 30, 2018

Getting Started with Angular and Visual Studio Code

Download and Install

Visual Studio Code (could use Visual Studio also or many other editors)
Angular CLI
Angular Quickstart App


Open the View -> Interactive Terminal and type npm install to install all the packages specified in packages.json. Files will be put in node_modules which can safely be excluded from source control if desired since it can be rather large.

Running your app

Open the View -> Interactive Terminal and type npm start. This will build, start the web server, and open the browser.

Stopping your app

Go back to the Interactive Terminal and type Control-C and then Y to stop the web server.

Making Changes

If you make changes to the .html files the changes are automatically updated in the browser.

Friday, January 26, 2018



If you are in an async method and you want to have it wait for a period of time before continuing your first thought might be to use Thread.Sleep(), but this would work the way you may think. Task.Delay() is what you want to use. Sleep just sleeps on the current thread which is the same as the thread that is calling the async method. Having an async method does NOT create threads unless the


Task.Run() method is used to run a non-async method on another thread (from the thread pool by default). This is essentially just a background thread and has nothing to do with async. However since Task.Run() returns a Task you can await the result. This makes tracking the task and getting the result very easy.

Task.Run() is a good way to run a non-async method from an async method. There is overhead of spinning up an additional thread just to run this non-async code and then we would await the completion of the thread. This overhead is sometimes the only option if there is no async method available to call and all you have is a non-async method to call and you don't have access to change it.

Monday, October 16, 2017

Async in C#


A thread is a relatively heavy weight structure. 1 thread takes 1M of Stack Space. A task is NOT a thread. You can have multiple tasks running on a single thread (such as UI thread).

Task.Delay() is like Thread.Sleep(), but it is a Task that won't complete for a specified amount of time.

The keyword async is not part of the method signature. For example, it doesn't show up in interfaces. It is convention to add Async to the end of a method name though. This tell the reader it is async.

Return Task, not void.


Frees thread to do other things such as update UI, etc. Before await is called the stack trace is in the method you called it from which makes sense. The potentially surprising thing is that the line after the await does NOT have the same stack trace. It is now in the same thread, but clearly not in the same calling method anymore. This is because the compiler creates a state machine when the code is complied. It's building what would have been done by hand before we had the keyword await in C#.
  • await frees a thread while we wait for an operation to complete. For example, handle another request in web scenario, update UI thread in desktop scenario. 
  • It does NOT block the thread, it actually frees the thread to do something else. 
  • The execution is stopped until the task is complete. 
  • When the task is complete the thread gets focus and execution continues. This is similar in concept to a callback after something is complete.

Exception Handling

If there is an exception in an async method it will be thrown and can be caught as if it was not an async method. For example, a simple try-catch.