Thursday, October 19, 2017

Git: Setting Name/Email from the Command-Line


After logging into to git using your credentials, git knows  you are logged in but does not always know who you are with respect to your name and email. Adding this to  your global git settings is a matter of invoking git config --global as follows:

git config --global user.name "Jan David Narkiewicz"
git config --global user.email "jann@sofwarepronto.com"

Obviously, users should specify their own name and email address when  updating git's global configuration settings.



Monday, October 16, 2017

Windows 10: Logging out of the current Git account

I find myself working from multiple Git accounts while logged into the same Windows login. Normally I use Git exclusively from the command-line but when logging out of Git to insure I can login and receive a new login-prompt, I use Control Panel, specifically the dialog for managing Windows Credentials -- see path:
Control Panel | User Accounts | Credential Manager | Manage Windows Credentials

In order to access this functionality, display Control Panel:


Select User Accounts which displays the following:

Under Manager your credentials select Windows Credentials:


 Notice in the previous screenshot that there are git related credentials under heading, Generic Credentials. To the right of git:https://github.com click on expand button,


Clicking on this button reveals the following:


The Remove button removes the credential. Remove all git related credentials to be force git to prompt for a new user login.

Sunday, October 8, 2017

C#: Running Google OAuth 2.0 for Desktop Apps on Windows 7

This article demonstrates the code required to make Google OAuth 2.0  for Windows desktop applications work with Windows 7.

Google OAuth 2.0 means that an application can relying on Google's user credentials in order to support authentication. The users of an application can use their GMail address and corresponding password to login to a non-Google application. In order to implement OAuth 2.0 support, there are variety of steps that must be taken from an application registration process and a development standpoint.  Google has created extremely comprehensive documentation with regards to supporting this authentication protocol, "OAuth 2.0 for Mobile & Desktop Apps" and has also provided series of examples on github.com, https://github.com/googlesamples/oauth-apps-for-windows.git, that target a variety of Windows desktop configurations. The problem with the Windows desktop samples provided is that they fail on Windows 7.

The solution from example Windows console application from the aforementioned github.com, URI appears as follows in Visual Studio's Solution Explorer:



When this applications run on Windows 7 an exception is thrown and an "Access is denied" error message is displayed:


The specific line at which exception is thrown is line 78 in the following screenshot (the Start method of an instance of the HttpListener class):


The mechanism used to fix the Access Denied error is to "Reserves the specified URL for non-administrator users and accounts" (see "add urlacl") by means of the netsh command with the http add urlacl arguments. Basically this means, the user has permission to create an HttpListener instance and to listen on the URl by invoking HttpListener.Start. In invoking netsh to facilitate URL access is handled by the Process class's (see the System.Diagnostics namespace) Start method. An example of invoking netsh using Process.Start to allow a user to access a specific URL is show as follows:

public static void EnableAccessOnWindows7(string url)
{
    string domain = Environment.UserDomainName;
    string user = Environment.UserName;
    string commandArgs = 
             $"http add urlacl url={url} user={domain}\\{user}";
    ProcessStartInfo startInfo = 
             new ProcessStartInfo("netsh", commandArgs);

    startInfo.Verb = "runas";
    startInfo.CreateNoWindow = true;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
    startInfo.UseShellExecute = true;
    Process.Start(startInfo).WaitForExit();
}

The EnableAccessOnWindows7 method is invoked by Google OAuth 2.0 Console Application sample at line 68 to allow the sample to run on Windows 7:


Given the changes made the sample now runs on Windows 7.

Source Code

Normally source code for these posts is made available through github.com. Google owns the sample reference and maintains a master copy in their own github.com repository: https://github.com/googlesamples/oauth-apps-for-windows.git. The code associated with the modifications required to allow the desktop samples to run on windows 7 are included in the body of this post.