Skip to content

Instantly share code, notes, and snippets.

View RhysC's full-sized avatar

Rhys Campbell RhysC

  • Perth; Australia
View GitHub Profile
@RhysC
RhysC / AnonUsersInAsp.cs
Created July 9, 2013 04:17
Handling Anon users in ASP.Net. You can use the Anon module (include it in your web config and the anon users will get a unique token (cookie) that can be used to track them. On authentication we can handle the event of changing from anon => auth and clear the token http://brockallen.com/2012/04/07/think-twice-about-using-session-state/ http://m…
using System.Web.Mvc;
public class HomeController : Controller
{
public ActionResult Index()
{
if (User.Identity.IsAuthenticated)
ViewBag.Message = "User = " + User.Identity.Name;
else
ViewBag.Message = "AnonymousID = " + Request.AnonymousID;
@RhysC
RhysC / PollWebsite.ps1
Created July 5, 2013 01:43
Poll URL to see if it is up.
function Poll-Website([string]$uri){
$status = ""
$retryCount = 0;
while($status -ne "OK" -and $retryCount -le 5){
try{
$status = (Invoke-WebRequest -Uri $uri).StatusDescription
Write-Host $status
}
catch
{
public class FakeControllerContext : ControllerContext
{
public FakeControllerContext(IController controller)
: this(controller, null, null, null, null, null, null)
{
}
public FakeControllerContext(IController controller, HttpCookieCollection cookies)
: this(controller, null, null, null, null, cookies, null)
{
@RhysC
RhysC / SendMessage.cshtml
Created June 12, 2013 00:45
Azure storage queue (not service bus) demo using mvc and worker role
@using (Html.BeginForm())
{
@Html.TextArea("content")
<input type="submit" value="Send it!" />
}
@if (ViewBag.SentMessage != null)
{
@RhysC
RhysC / DateSelector.js
Last active December 18, 2015 04:48
Knockout bindable date selector view model using moment to assist with date parameter and ensuring valid dates are shown. This code only shows valid dates in the date range supplied. Dependencies : knockout, moment
/// <reference path="~/Scripts/ThirdParty/knockout-2.1.0.debug.js" />
/// <reference path="~/Scripts/ThirdParty/moment.js" />
function YearSelector(earliestDate, latestDate) {
var self = this;
self.earliestDate = earliestDate;
self.latestDate = latestDate;
var getYears = function () {
var years = [];
@RhysC
RhysC / GetInstalledApps.ps1
Created April 7, 2013 01:41
Get all installed apps on current machine
#32 bit path - "hklm:\software\microsoft\windows\currentversion\uninstall"
$regInstallPath = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
$installed = gci $regInstallPath |
foreach { gp $_.PSPath } |
select DisplayVersion,InstallDate,ModifyPath,Publisher,UninstallString,Language,DisplayName |
where { ($_.DisplayName -ne $null) -and ($_.DisplayName -ne "") }
@RhysC
RhysC / TestAssociation.cs
Last active December 15, 2015 00:59
Simple attribute and reporting for what tests are related to which work items (such as PBI's, QA Testcases, Tasks etc.... whatever you want really). Nothing magic, you just mark your test class or method with the attribute and put in the dataz.
void Main()
{
WorkItemReporter.GetTestCoverageReport(typeof(MyTest).Assembly).Dump();
}
// Define other methods and classes here
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class WorkItemAttribute : Attribute
{
public WorkItemAttribute(WorkItemType workItemType, string id)
@RhysC
RhysC / DeafultWebFormInputValidator
Created March 16, 2013 06:59
html forms validation based on the teststack.seleno page model. Faked out page and assert, need to adjust to fit. This is designed to work with DynamicCsv Gist.
//Will need some rework and some decent test runs. IDeally generate the start of the csv too.
public class PageFormTestInput<T> where T : new()
{
private const string isFormValidFieldName = "IsFormValid";
private readonly dynamic input;
public PageFormTestInput(dynamic input)
{
this.input = input;
}
@RhysC
RhysC / DynamicCsv.cs
Created March 15, 2013 09:50
CSV reader from http://www.thinqlinq.com/default/LINQ-to-CSV-using-DynamicObject.aspx. Ported to c# with basic usage example at the bottom (using xunit). Uses dynamic to provide the field names based on headers. Have changed the default of changing spaces to underscores to just removing them.
using System;
using System.Dynamic;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using Xunit;
namespace YourNamespace
{
public class DynamicCsv : DynamicObject
@RhysC
RhysC / MVC View Model Fixture.cs
Created March 5, 2013 10:41
Simple attribute testing on MVC ViewModel
public class PesonalDetailsFixture
{
[Fact]
public void RequiredPropertiesAreMarkedAsSo()
{
PropertyOn<PersonalData, string>(x => x.Title).Has<RequiredAttribute>();
PropertyOn<PersonalData, string>(x => x.FirstName).Has<RequiredAttribute>();
PropertyOn<PersonalData, string>(x => x.LastName).Has<RequiredAttribute>();
PropertyOn<PersonalData, DateTime>(x => x.DateOfBirth).Has<RequiredAttribute>();
PropertyOn<PersonalData, string>(x => x.Email).Has<RequiredAttribute>();