Non-Domain Management of Hyper-V Server 2016 with Windows 10 Hyper-V Manager

While Microsoft lays out the process in an MSDN article, I felt the article left out a bit of information.  Especially for any developer who may not have prior systems administration experience.   So below I’ll attempt to expand on the content MS has provided in this article

Step 1:  Enable PS Remoting on the Host.

In order to do this properly you need to be in powershell on the Hyper-V Server.   So login to the Server

hv-desktop
Hyper-V Server’s glorious desktop

Now that you are logged in, you need to launch powershell.  You can either enter 14 to Exit to the command line, or you can simply toggle to the cmd.exe window that is already opened behind the sconfig window.  Once you are at the command line simply type powershell and press enter.  This will start powershell.

Starting Powershell
Starting Powershell

Once you have the PS command prompt you are ready to enable your environment for connectivity.

Now you enter the command Enable-PSRemoting  at the Powershell prompt. This will not provide feedback. It will simply do its job then drop you back at the PS prompt.  After which you will enter Enable-WSManCredSSP -Role server to enable Remote Management.  This one will provide feedback and a confirmation prompt.

Step 2:  Configure the Windows 10 client to support the host connection.

Since the machine is not on the domain, I don’t actually use a FQDN. Instead I use NetBios.  However, I typically include the entry in my hosts file because I find NetBios to be a little flaky.   Even so, I also sometimes find it easy to just connect using the IP address.  Since this Windows 10 + Hyper-V 2016 setup supports that, I configure it both ways (Note: I’ve never actually tested the IP address connection so I don’t know if it works).

As listed in the article, you have 2 commands and a gpedit in your future. First, as instructed in the article, issue the following command:

Then, optionally for IP support, issue this command as well:

With the Trusted Hosts added, now you enable credssp on the client, exactly as the article indicates.

I think this is supposed to add an entry to the server list in the group policy for Allow delegating fresh credentials with NTLM-only server authentication. However, it seems that it sometimes adds it under Allow delegating fresh credentials instead.  So I found it was necessary to manually edit both of these and ensure the following entries were in my server list.  Again, if you don’t want to include IP support you can drop that one.

Policy editor allowing delegation of fresh credentials
Policy editor allowing delegation of fresh credentials

If you are not familiar with group policy, you simply click start and type gpedit and it will appear in your search results.  Both of these group policy settings are found under Computer Configuration > Administrative Templates > System > Credentials Delegation 

After this I was finally able to get into the machine remotely to manage Hyper-V.

This was a little bearish to setup even with the instructions.  I hope this will help someone else who encounters the same struggle.

Turtle Copy

Turtle copy is a tool I recently created for the sake of copying files slowly.  Why would you ever want to copy files slowly you ask?  Because some test scenarios make it necessary.  For example: “How would this file processing subsystem work if the incoming file was transferred over with a CPU that was near max usage?  Or maybe a network connection that is troubled?”

It’s not polished or fancy, purely a command line utility and at the time of this writing I know of at least one minor bug.   However, it serves me well enough and I thought it may help someone else.   So here’s the source code.

Adding Recaptcha to ASP.NET MVC with no baggage

When I decided to add recaptcha, to an email contact form, I naturally started looking online at options for completing the task. There were many routes I could have gone, but it seemed that most of the blog sources out there all pointed me to a nuget package, or to “go to codeplex and get this thing” . Then, when reviewing the documentation for those ‘helper’ projects, all of the processes seemed convoluted, and disappointing. I knew there had to be a simple, and pure, approach and so I decided to go to the source: Google.

Google is great at many things. I generally think their documentation is solid as well, but honestly, the docs for recaptcha are fairly weak. They contain all of the needed information, but in a way that is only slightly clearer than mud.

However, using what they gave me, and some creativity, I was able to arrive at a working solution. I will explain it all below:

Inserting the Captcha

First, as Google instructs you to do, add the following html where you want the captcha to appear. Along with the JavaScript (at the end of your html body). Note: Pay attention to the data-callback attribute that is not shown in Google’s example, but described in their surrounding text.

So that will actually show you the captcha if you try it just as it is. However, we have to validate it as well.

Working with the Captcha Client Side

In order to do that you really have a two step process. When the user interacts, the callback method you specify in that data-callback attribute is triggered. However, the value is simply an encoded response, and you will pass it back to the server as-is. To best accommodate this, I added a @Html.Hidden field to my page, and mapped it to a string property in my view model. Then, using the handleCaptcha() call back method, I set the response from Google into that hidden field.

So now you can present the captcha to the user, and capture the response contained in the call back, and send it to your controller for further validation. For this final validation you make a final api call (as described by Google) to get a yes or no answer.

Bringing it all back to the server to validate securely

For that I simply added a subroutine that returns a boolean, and call that in my controller passing the Google response string. Note: The response from the API call is a JSON object that .NET deserializes to a hashtable (technically a dictionary<string, object>). You find the “success key” and its value is your boolean success indicator.

That was it, it works exactly as I would have expected, and is quick and painless. I also added logic to disable my submit button until the captcha callback took place, that way the user couldn’t accidentally submit the form without that response.

 

 

SVG Manipulation using JQuery

This started out as a simple idea. I have an Scalable Vector Graphics (SVG) image of the United States. I have data for several of those states. I want the state to be colored for the data, and to show the data in a Bootstrap popover on hover.

After doing a limited amount of research, one thing was clear, I needed to make sure I had the IDs set on my image correctly, and to do that I had two options. One was to edit the svg in a text editor – which would be fine except that I don’t know which path is Kentucky and which path is Oregon. So this could have created some serious confusion when you hover over California and see a popover for Maryland. Option two, was to use an SVG editor, and for me that is Inkscape. To do this in Inkscape, you simply select an element and press CTRL + SHIFT + O. This will bring up the object properties dialog (also accessible via the Object menu), and here you can provide an ID and a label for the shape. I had to do this for every shape with which I wished to interact. So, there were 50 total.

Attempt #1: (Note, this one was only a partial success – hence, the #1):

I pulled in the SVG as an object. Then using JQuery I was able to load the contents of the object. From there I was able to use selectors to access the id’s I added, and I could change the colors of the items – however I couldn’t get the popovers to fire. I tried several different approaches, and each one failed. My theory is that while I can traverse the SVG document object model to update fill styles, I can not tie it back to my HTML DOM in a meaningful way.

Arriving at this conclusion, I decided to paste that beast of SVG code into my HTML markup, which leads me to Attempt #2.

Attempt #2: (Success, but not my final solution)

So, now my HTML is huge, because it contains a massive amount of SVG data, but all of the elements and their id’s are part of the same DOM as my HTML markup now. Using the following HTML + JQuery I am able to access the document and do pretty much whatever I want with it.

First, the HTML

And the Script

 

Attempt #2.1 (My final solution)

So, for the final solution I wanted to get that nasty SVG code out of the markup. Because I realize that, while it doesn’t ease any bandwidth burden, it’s still more readable if you are viewing the HTML source. The only change there is to add a div, with a custom (in my case data-svg) attribute that points to your SVG file, then with JQuery you do the following

 

I created a similar project, specifically to share here, and it can be found on GitHub. I will try to post a demo and update this post in the near future.

Simple windows script to delay application startup

I created an application that needed to run on startup, and needed to run as admin, and do it all on XP. The post about it was started a while back, but is yet to come – once it’s finished proving itself worthy. However, when executed on certain machines there have been issues where the startup process for the OS wasn’t finished in time for the application launch. These issues resulted in permissions errors. These errors didn’t occur if you closed and reopened the application seconds later.

The simple solution became obvious after a few moments of consideration. I simply needed to delay the startup of the application. To do this I crafted a very simple VBScript and placed it in the all user’s startup folder, replacing the shortcut that my installer had placed there previously.

Note: To simplify the example below uses calculator as the application to launch, and it assumes the Windows Directory is c:windows. In the real implementation this was simply the same path that was referenced by the original shortcut.

And that is it. A very simple script that solved a major problem. After the delay, the application starts every time without issue. This small endeavor was the first time that I’d every truly realized the potential in visual basic scripting in a Windows environment. I’ve used PowerShell, and Batch files, and Console applications many times, but this scenario

Java to WCF Service Error: Two declarations cause a collision in the ObjectFactory class.

I don’t talk about java on here too much, because I avoid it most of the time. I was never a big fan, but concede that it has it’s usefulness. After all: It powers devices of many shapes and sizes, it is platform agnostic, and it has a ton of community support. However, I’ve always found the IDEs to come up short of expectations — Visual Studio set this bar very high. Anyway, it was just recently that I had a good reason to dig into consuming a WCF service from a java client. This foray into the deep dark recesses of JAX had me ranting about the evils of java.

“As a .NET guy, I am convinced that JAVA was spawned in the depths of a cavernous inferno. A place where the pervasive stench, of all things unholy and unclean, consumes all it contacts. A place so vile that the darkness remains unerringly fast, even as the sun illuminates the surrounding lands.” — Me via Facebook

What powerful force could have driven me to such frustration, that I would publicly make such statements? This obscure error message:

[ERROR] Two declarations cause a collision in the ObjectFactory class. Line 1 of file...

The error in and of itself is innocuous enough, two proxy objects are trying to generate with the same name. However, I do not have two objects named the same thing, even if I was foolish enough to create that scenario, C# would not allow it. Perhaps I could have created the collision by assigning data attributes incorrectly. However, since the WSDL came across as a single line, I didn’t have a hint as to where to look.

Eventually I copied the WSDL to a text file, broke it out line by line, and attempted to regenerate the proxies. After this exercise, I finally had something to work with. The two lines were there, but they certainly were not named the same thing, not even close. One was named MailingAddress, the other was named MailingAddressZipCode. What madness is this?

So I popped open my service source to discover the following object definitions (shortened for brevity).

When I realized the problem, I second guessed it. Surely the proxies would not be generated in such a way that they drop scope resolution on the floor. MailingAddress.ZipCode is not the same thing as MailingAddressZipCode. However, the voodoo that JAX employs when generating these proxies fails to care. After a quick renaming of the properties I confirmed this to be true. I renamed the MailingAddress object to PostalAddress, and viola proxy generation succeeded.

So the lesson here is simple. When creating a WCF service, if you expect connectivity from java clients, you must not name classes anything that may potentially clash with a concatenated class + property elsewhere in code.

For a language that runs on so many devices, and has – and has had for so many years – so much potential in the marketplace, it’s things like this that makes me and many other developers run away from java except when it is the only choice. Well this and the fact that an equivalency operator is not valid for string comparison.

Making Bootstrap Even Easier, with MVC HTML Helpers

Inspired, more than a little, by a blog post over at mvcdiary.com, I decided that I could make my life with bootstrap even easier. I had the idea to create HTML Helpers for MVC that would enable the use of some of the display attributes to add more auto-magic goodness to the page.

My goal was to be able to simply use @Html.BootstrapTextBoxFor(m => m.description) in my markup, but the rendered output would include the bootstrap placeholder, and the title attribute would be set to allow for automatic tooltips on mouse over.

I decided that it would make a lot of sense, to use the “description” value of the display attribute for the tooltip, and the “prompt” value of the same attribute for the placeholder. So I started off — as I usually do — to see if anyone had already done this. Sure enough, I found the blog post, which gave me an incredible head start. The grunt work was already done, it was just assigning the additional properties. Here is the final result (feel free to compare to the original).

The result is exactly my desired result.

Markup:

Model:

Output:
screen-snag-post-62

It was all very easy to implement, and extend even further and to other standard helpers, such as the textareafor helper.

Opening Illustrator Generated SVG in Inkscape

I have a file that was sent to me from a friend, and it was saved as .svg, but wouldn’t open in Inkscape.

I attempted to both, open it directly, and to “import” it, with no success. So I opened it up in notepad++ and found that it was generated with Adobe Illustrator 11. Armed with that information I renamed the extension to .ai.svg, which I had previously noticed was a special import option in Inkscape. Once it was named as .ai.svg it imported just fine using the Adobe SVG import option.

I just thought I’d share this tidbit in case anyone else runs into it. It was a super simple work around, and now I can tweak the SVG in inkscape.

EF5 Code First with Multiple Databases

This turned out to be fairly simple, but it took me a while to figure out what was going on. I have two databases configured for one application. One is an authentication, authorization, and loggging datbase – it makes use of the default security framework with ASP.NET MVC 5 projects. In addition it utilizes NLog for logging. The second database is for application data.

So essentially I have two connection strings:

 

I have the default IdentityDbContext (included by default as part of the IdentityModels.cs file ) from an “out-of-the-box” MVC 5 application (with individual user accounts). To this, I added a new class “Log”, and included it in the context by simply adding a new DBSet to the properties of the context object.

In addition I added a new DbContext subclass, AppDataContext, with my two initial objects (very early in this project).

So my goal here was to simply be able to keep both contexts alive, and use package manager to add-migration(s) and update-database(s) for each connection string. However, out of the box this immediately runs into issues. Once you enable migrations for one, you must rename it before you can enable migrations for the other, because it wants to create the file with the default template, and you already have a configuration.cs file. To the credit of the product, they don’t let you do this without warning. However, that is not the desired result for me. I need to have both alive and well.

So I simply renamed Configuration.cs to IdentityConfiguration.cs, then enabled migrations again specifying the AppData context. And I got my second file. For the sake of consistency I renamed it as well, to AppDataConfiguration.cs. However, this is not sufficient. Because now, when trying to add migrations, the first one adds fine. The second, however, insists that you apply the first before you can continue.

Of course this made sense, since with EF Code First you have to apply a migration before you can add another. So I simply popped into the package manager and ran update-database providing the -configurationTypeName pointing to my IdentityConfiguration.cs file. This worked swimmingly, so I went back to add my migration for the AppDataConfiguration, when I was told that it was necessary to apply a pending migration. Since I’d just applied my only pending migration, this didn’t sit well with me. So I turned to the internet and found a thread on Stack Overflow (a site I highly recommend), which helped me resolve the root issue.

Even though I’d changed the names of both files, and classes, I had kept them both in the same namespace. Apparently, for EF CF Migrations, this is not cool. So I changed the namespaces and class names (to make it easier) from

AND

To:

AND

Respectively.

This was the trick, it all works fine, and as I’d expected, once you put everything into the distinct namespaces. While it is an annoying issue, at least it’s a very simple work around. By applying this fix you can have many database connections managed by EFCF.

Converting Image Hotlinks to Azure Storage Images

While working on a current project, I found myself with a need to cache media that I was collecting from various websites to my Azure Storage account. Then utilize the storage as somewhat of a Content Delivery Network (CDN) for my images and media.

I found a good article online about how to configure the storage account as a CDN. Which told me very quickly how to set it all up and configure it for my purposes. So the only challenge was to implement the logic to pull the images in there.

The following function pretty much does it all. First there is a field that is included in my model called “MediaUri”. This field is populated on the view with a URL for an image out on the web, say from Wikipedia for example. However this function, will convert that hotlink URL into a cdn.monkeyboxtech.com URL after downloading and caching the image to the CDN storage area.

Note this example relies on the Azure SDK tools being installed.

Then, in my web.config file for this application I have an app settings key that helps connect the cloud storage account. Of course you’d replace the <omit> with the actual values.

That is pretty much all there is to it. By using the Azure SDK tools for .NET, caching an image for later hosting from your storage account couldn’t be much easier.