Wednesday, August 27, 2014

Style all <select>'s with a given CSS class into a fancy radio button look

Json.Net Serialize Dictionary<Tkey, Tobject> to List<Tobject>

Motivation: Wanted convenience of a Dictionary on the server side MVC controller (for some key look-up based logic), yet send the same list of objects down to Knockout Ajax client, which most readily consumes lists as Javascript object arrays.

Could've just exposed the List<Object> as another property but wanted to see if I could roll it all into one property just to learn a little more about the Json.Net API.

Nice that Json.Net's tokenization framework has already abstracted away from specific types within the overridden method scope on custom JsonConverter class, so this approach generically handles *any* Dictionary<object, object> without any additional effort.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace YouNameIt {

  using CartItemDict = Dictionary<long, CartItemDto>;

  //reverse of this:
  //adapted from here:
  public class DictToListConverter : JsonConverter
    public override bool CanConvert(Type objectType)
      return objectType == typeof (Dictionary<object, object>);

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      throw new NotImplementedException();

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      var t = JToken.FromObject(value);

      if (t.Type != JTokenType.Object)
        var o = (JObject) t;
        var values = o.Properties().Select(p => p.Value).ToList();
        (new JArray(values)).WriteTo(writer);

  public class CartDto
    public CartItemDict Items { get; set; }

    public decimal TotalPrice { get; private set; }

  public class CartItemDto : VariantDto
    public long CartItemId { get; private set; }
    public int Quantity { get; private set; }

    public decimal UnitPrice { get; private set; }
    public decimal LinePrice { get; private set; }

    private List _addons;
    public List Addons { get { return _addons ?? (_addons = new List()); } }

Friday, July 4, 2014

[SOLVED] Comodo v7 blocking HTTP/S and FTP/S on Windows 8.1 IIS 8.5

Besides opening incoming HTTP ports in the firewall via "Global Rules", the annoying thing for me to find was also adding an "Application Rule" for "Windows Operating System" on those same ports.

Comodo v7.0.317799.4142

And this guy explains what's necessary for FTP very nicely...

  • in comodo > global settings > application rule - add 20,21 & 5000-6000 as allowed incoming TCP ports on "Windows Operating System"... you will also hopefully get prompted to allow svchost which is responsible for running the ftpsvc
  • on internet router - forward ports 20,21 and 5000-6000
  • in IIS FTP settings
  • filezilla settings
    • require explicit ftp over tls

Wednesday, June 11, 2014

List all your Azure RDP's

Get-AzureVM | #this first one gets the entire list of VMs in subscription
    Get-AzureVM | # this one gets the detailed object for each specific VM
        $port = ($_ | Get-AzureEndpoint | ? {$ -like "Remote*"})[0].Port;
        $null = $_.DNSName -match 'http://(.*?)/'
        write-host "$($_.Name) - $($matches[1]):$($port)"

Saturday, February 8, 2014

SQL Server Aliasing

  • Done via "SQL Server Configuration Manager" > "SQL Native Client vXY.Z Configuration" > Aliases
  • tip: SSMS.exe is a 32bit app (because Visual Studio, upon which it is based, still has a well established justification for 32bit) and therefore it depends on the (32bit) Client Configuration node above to find your server alias
  • For mainstream sql server network client API based connections there is no need to put this alias anywhere else (i.e. not in DNS/hosts file nor AD computers)
  • tip: in AD trusted login context, it seems mandatory to use the name of the actual SQL Server host machine vs just the corresponding ip address; otherwise i would always get bonked with "Login failed. The login is from an untrusted domain and cannot be used with Windows authentication."

Monday, January 20, 2014

[SOLVED] Win8.1 Upgrade - No "Keep Windows settings, personal files, and apps" option

I was met with only two options from the Windows 8.1 upgrade, "Keep Personal Files Only" or "Nothing". Not much of an "upgrade", I went poking around.

For me it turned out that I had been fiddling with localized development a while back and had an old en-GB language pack still installed. There are various references that the Win8.1 upgrade criteria prohibits "cross language" installs.

Apparently a language pack can't be removed from a running Windows instance, it must be "offline". One way is from the CMD.exe of a Windows DVD/USB install boot disc. Tip: Shift-F10.

After :)
To find which language packs are installed ("Language" is case sensitive):
dism /image:c:\ /get-packages | find "Language"

Which output something long like this:
Package Identity : Microsoft-Windows-Client-LanguagePack-Package~31bf3856ad364e35~amd64~en-GB~6.2.9200.16384

To remove the package:
dism /image:c:\ /remove-package /packagename:{long_name_from_above_output}

That ran for a few minutes to completion and when I booted back into my main instance and retried the upgrade, I was met with the new desired option to preserve my applications as well - yay :)

Monday, December 23, 2013

KeePass + Cloud Storage = (near) Password Nirvana

KeePass2 - Password management application
  • 10 years mature
  • Free
  • Windows, Linux, Mac, Android and iOS versions
  • DropBox compatible (Google Drive, etc)
  • Autofill browser plugins
  • Rich text area for notes (e.g. challenge phrases and other reminders)
  • Open source (.Net)

  • Mac (and Linux) can run the Windows.exe via Mono
    • Initially ran native KyPass Companion on the Mac side (~$8). Have since switched back to the free mainstream build (see below)
  • On Android phone using Keepass2Android (free) with solid results
    • Provides special keyboard which facilitates autofill
    • DropBox and other cloud drives well supported (synchronize)
    • Handy yet still secure Quick Unlock feature
    • Consider a good android lock screen as additional layer of protection

I’m glad I finally took the time.  I (forced ;) my wife to run the Windows version on her desktop and we share the same database file with our financial, healthcare, etc logins. So either of us can get into whatever we need wherever we are. It gives me peace of mind that she would have ready access to those important things in case I was somehow unavailable (knock wood). If you're putting up with some other convoluted hodge podge as I was, please give this general idea a shot by wading in slowly and see if it makes your life easier as it has for me.

 2013-01-01: My main password file was corrupted

and I couldn't log in.
  • Turns out I had a wonky entry that kept growing upon subsequent saves. Maybe compression algorithm was backfiring or something like that.
  • The offending entry was under KeePassHttp which just stores the authorized connection for each particular browser, so it was a no brainer to kill and recreate.
  • My kdbx file had grown to 28MB! after deleting it was back down to a measly 16k.
  • KyPass Companion was doing the most recent suspect saves causing massive growth so I can't help but wonder.

DropBox really shines
  • Thanks to DropBox's inherent versioning I could readily fallback to a working copy
  • Dropbox also showed the disturbing progression in larger file sizes over short amount of time
  • as well as which client that was driving those suspect saves - KyPass on my Mac
  • really gotta hand it to that product team, top notch stuff
KyPass's questionable involvement gave me a reason to give the mainline KeePass2 another look...

    Banging KeePass2 for OS X into shape
    • Updated from current v2.23 build to the latest official v2.24 build by dropping the latest KeePass.exe from the Windows zip bundle into the Contents/MacOS folder. This is promising; hopefully to never suffer the envy of a more recent build.
    • Contents/MacOS is also where plugins like KeePassHttp.plgx should be dropped.
    • KeePassHttp is working just fine for me running under this mono version.
    • Make sure to disable "Show a notification when credentials are requested" under Tools > KeePassHttp Options. Otherwise both KeePass and browser would freeze upon every login page request.
     Nice to have's in KeePass not currently available in KyPass Companion:
    • Automatic save-on-change (via triggers facility)
    • Autoload of the MRU kdbx file upon launch
    • Synchronization

    [SOLVED] Error: "The following plugin is incompatible with the current KeePass version"
    • Running on Mac via mono, turns out lldb is somehow the process forked by mono which hosts the KeePassHttp listener on port 19455
    • In my situation this pesky error was apparently caused by a crashed orphan lldb holding onto the port and blocking subsequent launches of KeePassHttp
    • Simply "KILLALL lldb" from terminal to resolve
    Debug notes:
    • mono>debug.txt -v /Applications/KeePass{version}/Contents/MacOS/keepass.exe
    • Noticed SocketException well into the KeePassHttp plugin's constructors call stack and started to realize the error message was misleading
    • Xamarin Studio will debug the running instance:
      • First, enable debugger break on SocketException: Run > Exceptions > enter SocketException in the search
      • Run > Debug Application > browse to keepass.exe
    • Xamarin Studio will also reverse gen back to C# source (not that we need it in this case but it's good to know for future) - just create a new project and add the assembly (DLL or EXE) as a reference and click into it to see the readable source conversion of all classes.