Skip to content

Instantly share code, notes, and snippets.

@harrybiscuit
Created May 28, 2013 21:14
Show Gist options
  • Save harrybiscuit/5666197 to your computer and use it in GitHub Desktop.
Save harrybiscuit/5666197 to your computer and use it in GitHub Desktop.
[Subject("Viewing Secured Home Page")]
public class when_viewing_a_secured_homepage_with_no_auth_cookie
{
private static HttpWebRequest request;
private static HttpWebResponse response;
private Establish context = () =>
{
request = HttpWebRequest.Create("http://localhost:53759/") as HttpWebRequest;
request.AllowAutoRedirect = false;
};
private Because of = () => { response = request.GetResponse() as HttpWebResponse; };
private It should_redirect_to_the_login_page = () =>
{
response.StatusCode.ShouldEqual(HttpStatusCode.Redirect);
response.GetResponseHeader("Location").ShouldEqual("/Account/LogOn?ReturnUrl=%2f");
};
}
[Subject("Viewing Secured Home Page")]
public class when_viewing_a_secured_homepage_with_auth_cookie
{
private static HttpWebRequest request;
private static HttpWebResponse response;
private Establish context = () =>
{
var authCookie = TestCookies.GetAuthCookie();
var cookieContainer = new CookieContainer();
cookieContainer.Add(authCookie);
request = HttpWebRequest.Create("http://localhost:53759/") as HttpWebRequest ;
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(authCookie);
request.AllowAutoRedirect = false;
};
private Because of = () => response = request.GetResponse() as HttpWebResponse;
private It should_return_the_home_page = () => response.StatusCode.ShouldEqual(HttpStatusCode.OK);
}
using System;
using System.Net;
using System.Web.Security;
namespace LogIn.Specs
{
public class TestCookies
{
public static Cookie GetAuthCookie()
{
FormsAuthentication.Initialize();
var utcNow = DateTime.UtcNow;
var expirationUtc = utcNow.AddMinutes(2880);
var ticket = new FormsAuthenticationTicket(2, "fred", utcNow, expirationUtc, false,string.Empty, FormsAuthentication.FormsCookiePath);
var hash = FormsAuthentication.Encrypt(ticket);
var cookie = new Cookie(FormsAuthentication.FormsCookieName, hash)
{
Domain = FormsAuthentication.CookieDomain,
Path = FormsAuthentication.FormsCookiePath,
Expires = ticket.Expiration
};
return cookie;
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<machineKey validationKey="B1DA586F17AA1481EFD6E0327C33AA5486CBF56451E6C977C672DDF951D336A8ED1773A837638ACEDAE661A881B5E707079459D6BFF29EFD3E267BFFD3CA05A1" decryptionKey="1B2BB52CB6C4DD108DF7C68C11133C2E333B69B37353A72FE7EA9B88A5D50335" validation="SHA1" decryption="AES" />
<authentication mode="Forms">
<forms domain="localhost" loginUrl="~/Account/LogOn" timeout="2880" protection="All" enableCrossAppRedirects="true"/>
</authentication>
</system.web>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment