Thursday, 16 October 2014

How do I create a internet proxy for an account that does not have internet access?

The development domain for my current client does not given its account internet access. Thi can be overcome in Powershell scripts as follows:

$user = "MyDomain\MyAccount"

$password = ConvertTo-SecureString –String "password" –AsPlainText -Force
$cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $password

[System.Net.WebRequest]::DefaultWebProxy.Credentials = $cred

but it becomes a problem is the current user credential is used do download files or if the proxy server blocks certain actions. A fantastic tool to resolve this problem is cntlm, which allows you create a local internet proxy.

The configuration (simplified) is as follows:

1. Download and install the software.
2. Set the username and domain in the .ini file. Remove the password setting
3. Type the Url of internet proxy.pac file in a browser and get proxy urls. Add these urls to the config file and remove the 'sample' entries.
4. Execute 'cntlm -M' and get the authentication type. Copy the Auth and Password to the configuration file.
5. Remove/uncheck the 'Automatic configuration' settings and update the proxy in the browser to point to localhost 3128 (which is configured as the listen port)

6. Close existing browsers and restart the windows service.

You should be good to go.

SharePoint: How do I find out (quickly) if my server has a Kerberos ticket?

A new SharePoint project introduced the prospect of the dreaded 'double hop' problem and the team was looking at ways to resolve the issue. Naturally, Kerberos (and delegation) was the natural solution. However, we were not sure if the Farm and Servers had Kerberos enabled.

A quick one word line command resolved the problem: klink.

This command displays a list of currently cached Kerberos tickets.

Monday, 13 October 2014

PowerShell: How do I know what methods a commandlet has?

There is nothing more frustrating than trying to find out what methods are exposed in a commandlet that you are using. Fortunately, there is a commandlet to expose commandlets:


So, if I wanted to know what properties / methods are exposed in Get-SPOSite, I would execute the following:

Get-SPOSite | Get-Member

Thursday, 2 October 2014

MVC4: Why cant I resolve the WebSecurity class?

While trying to create a two step password reset page, I struggled to find the required reference for the WebSecurity class (to generate the ResetToken).

The solution is to add a reference to
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies\WebMatrix.WebData.dll

Wednesday, 1 October 2014

C#: Compare two text files (quickly)

I am busy writing a windows service that is send emails. However, I need to track the emails that have already been sent to that people. The current solution writes the audit to a text file (SQL Server is not an option).

I hit a major performance problem when processing a 30000 row file - I had already processed 5000 records and the iterative (line by line) approach was just not working. I put my SQL hat on and looked for a C# equivalent of the EXCEPT command.

Enter Googles Diff Match Patch.

It has a simple C# class that performs incredibly well. I now has a very simple and fast solution.

Here is the code:

using DiffMatchPatch;

string source;
string target;

// Load the source file into a string
using (StreamReader  reader = new StreamReader(MYSOURCEFILE))
  source = reader.ReadToEnd();

// Load the comparison file into a string
using (StreamReader  reader = new StreamReader(MYTARGETFILE))
  target = reader.ReadToEnd();

// Instantiate the class
var comparisonClass = new diff_match_patch();

// Perform the comparison
var diffs = comparisonClass.diff_main(source, target, true);

// Find all new items
var newRecords = diffs.FindAll(x => x.operation.ToString() == "INSERT");

I can now iterate through the new records and process them as required.

foreach (var newRecord in newRecords)
 // Insert magic here...