Software Development

Two Factor Authentication with GitHub and Visual Studio 2013

New job, new tools, new processes. In my new job we’re using GitHub for source control, and because the data is sensitive we’re also using two factor authentication. Because I develop with Visual Studio that presents and interesting issue if you are using Visual Studio 2013’s build in Git Source Control provider.

After turning on Two Factor Authentication, the next time you have to communicate with GitHub (e.g. pull/push/sync’ing, etc.) it will pop up a dialog asking for your credentials, even if you already entered them previously before turning on 2FA.

You get an error message that looks like this:

An error occurred. Detailed message: An error was raised by libgit2. Category = Net (Error).
Response status code does not indicate success: 401 (Authorization Required).

Entering your credentials won’t do you any good. It won’t work. It will just request them again, ad infinitum.

There is no where to enterthe 2FA code, so you can’t authenticate yourself here.

However, you can go to GitHub and create a personal access token in order that Visual Studio 2013 can access your repositories.

You can either drop down the menu on your avatar and go to “Settings”, then go to “Personal Access Tokens” (link in the side bar) or you can just go here https://github.com/settings/tokens.

Then click on “Generate New Token”. You’ll be asked for your credentials again just to be sure you are still you.

Once you’ve done that you’ll be taken to the page to create your credentials

For what Visual Studio wants the default permissions are fine. Also, give the token an appropriate name so it can be identified easily.

Then press “Generate token”.

You will then be taken back to the “Personal access tokens” page. This time there is a new token which you can use in Visual Studio. Be careful here, this is the one and only time you will be able to access this token so copy it and keep it safe.

Back in Visual Studio try and sync the commits to GitHub. It will pop up the credentials dialog again. This time you are going to enter the token in the username box and leave the password box blank.

Then press OK.

Finally, your changes will sync with GitHub and you’ll get a success message.

Tip of the Day

Tip of the day: Default project location in Visual Studio

When rebuilding a machine I always end up hunting this setting down just after installing Visual Studio because I keep forgetting where it is. I never put my projects in my documents directory. Ever.

In Visual Studio go to Tools –> Options... then navigate to “Projects and Solutions” and in the “General” section change the “Projects location” to the one you want.

Visual Studio Options dialog

Software Development

Previewing Config Transforms

Curiously, I didn’t know about this until recently and it is such a useful thing too.

You can preview the results of your configuration transformation from within Visual Studio.

First of all, right click the transform file and select “Preview Transform”

Then it will show you the differences between the original web.config file and the transformed file.

Tip of the Day

Really getting the latest changes with TFS

TFS Source Control doesn’t always get the latest changes. It gets what it thinks are the latest changes (and for the most part it gets it right if you work exclusively in Visual Studio). However, there are times when it gets it wrong and you have to force its hand a little.

So, if you have issues getting latest code then what you need to do is:

  • Right click the branch or folder that is problematic
  • Go to the “advanced” sub-menu and click “Get Specific Version…”
  • Then in the dialog check the two “overwrite…” boxes
  • Finally, press “Get”

At this point VS/TFS will retrieve all the files in the branch/folder selected and overwrite existing files. It will also retrieve files it didn’t already have, even although it thought it had them.

Tip of the Day

How to recover deleted files, folders and branches in TFS

In Visual Studio go to the menu item Tools–>Options…

Then navigate to the Source Control –> Visual Studio Team Foundation Server section.

In that section is a check box that says “Show deleted items in the Source Control Explorer”

Once you’ve ensured that the checkbox is checked, press “OK”

Then navigate to the Source Control Explorer and you’ll see that deleted files, folders and branches are now displayed with a large red cross next to them.

Right click the item you want to recover and select “Undelete” from the menu.

At this point Visual Studio stops responding to input. It displays a wait spinner briefly, but mostly it just looks like it has hung.

When Visual Studio does come back to life you can go to the Pending Changes to see the newly recovered files, folders, or branches.

If you are happy with this change, you can check it in to TFS as normal.

Software Development, Tip of the Day

There were build errors. Would you like to continue and run the last successful build?

Oh, Would I! Could I really do that?!?  Well yes, but I cannot think of any situation where I would want to do this. I’m not saying there isn’t a time I might conceivably possibly maybe actually want this, but I can’t think of it right now and I’ve not come across that situation for as long as I can remember getting this stupid dialog..

Now, obviously you can press the “Do not show this dialog again” and press “No” and it will remember that as your default choice. However, what if, like me, you were a bit ham fisted and accidentally pressed “Yes” and then wonder why your latest changes simply don’t work. How do I fix that? There’s no dialog any more.

The setting is accessible from Visual Studio’s Options dialog. You can get to by going to Tools–>Options:

In the options dialog go to Projects and Solutions—>Build and Run

There you will see the option "On Run, when build or deployment errors occur:" and a drop down indicating the action. Change the drop down to "Do not launch" in order to ensure that your application does not launch when a build error occurs.

While we are here, do you ever want to run your application when the projects are out of date? I can’t think of any time I’ve wanted to do that. There is an option to stop prompting you do do that and just “always build” in that case.

Once you’ve made your changes, press “OK” to save the changes.

Software Development

Node.js – Setting up a basic development environment

Node.js is a platform for executing JavaScript outside of the browser. It runs on the Google V8 engine which effectively means it is cross platform as it can run on Windows, Mac OS X, and Linux.

Since JavaScript written for Node.js is running on one platform issues that plague browser based JavaScript does not occur on Node.js. There are no “Browser Compatibility” issues. It simply runs ECMAScript 5.

Installing

To install Node.js download the installer from the node.js homepage. The “Install” button should give you the correct installer for the platform you are on, but if not, or your are downloading for a different platform you can get a list of each download available, including source code.

One thing I really like about the windows installer is that it offers to add Node.js to the PATH. Something that more developer tools should do.

Writing JavaScript for Node.js

While you can use a basic text editor, or even a more sophisticated text editor, IDEs are also available for writing node.js.

IDE – Node.js Tools for Visual Studio

There is a plug in for Visual Studio that is in beta (at the time of writing) which allows you to create node.js projects. This is very useful if you are doing cross platform development interacting with .NET based applications, or if you are simply used to working with Visual Studio. I like this option because I’ve been working with Visual Studio since version 2.1 (that came out in the early-to-mid-90s).

To get the extension for the Node.js tools for Visual Studio, go to http://nodejstools.codeplex.com/. If you don’t want to default download (which is for the latest version of visual studio) go to the downloads tab and select the appropriate download for your version of Visual Studio.

In the future, I would expect it to be available directly in Visual Studio through the Tools->Extensions and Updates… manager.

Once installed, you can create a new node.js project in the same way you’d create any new project. The Extension will have added some extra options in to the New Project dialog.

You can set break-points, examine variables, set watches and so on. The following is an example of a very simple Hello World application with a break point set and the local variables showing up in the panel at the bottom.

IDE – JetBrains Web Storm

If you don’t already have Visual Studio it may be an expensive option. JetBrains Web Storm is another IDE that allows you to create Node.js applications with a similar set of features to the Visual Studio extension above. Personally, I find Web Storm a little clunky but it works across Windows, Mac OS X, and Linux. If you don’t already have access to Visual Studio it is a much less expensive too.

Text Editors

Besides IDEs you can always use text editors. Some are more advanced than others.

The text editor I hear most about these days is Sublime, which can be more expensive than WebStorm depending on the license needed.  Sublime is a very nice looking text editor and it has its own plug-in system so can be extended, but for roughly the same money you could get a fully featured IDE.

Summary

I’ve not really gone all that much into the text editors available that support developing for JavaScript with Note.js because I don’t feel they really add much. A fully featured IDE with refactoring support is much more important to me. Maybe installing ReSharper corrupted me.