Skip to content

Instantly share code, notes, and snippets.

View shawnmclean's full-sized avatar
🐭
:)

Shawn Mclean shawnmclean

🐭
:)
View GitHub Profile
@shawnmclean
shawnmclean / HttpHeaderAttribute.cs
Created April 29, 2012 18:55
Setting Http Headers via attributes for ASP.NET Webapi
public class HttpHeaderAttribute : ActionFilterAttribute
{
/// <summary>
/// The name of the Http Header
/// </summary>
public string Name { get; set; }
/// <summary>
/// The value of the Http Header
/// </summary>
@shawnmclean
shawnmclean / basicwithsalt.cs
Created April 10, 2012 06:33
SimpleCrypto.Net Basic PBKDF2 Hashing with Salt parameter
private ICryptoService cryptoService = new PBKDF2();
private bool ValidatePassword(User user, string password)
{
//hash the password with the saved salt for that user
string hashed = cryptoService.Compute(password, user.PasswordSalt);
//return true if both hashes are the same
return hashed == user.Password;
}
@shawnmclean
shawnmclean / basicwithsaltsettings.cs
Created April 10, 2012 06:30
SimpleCrypto.Net Basic PBKDF2 Hashing with salt settings
private ICryptoService cryptoService = new PBKDF2();
private const int SALT_SIZE = 16;
private const int HASH_ITERATIONS = 50;
private void SetNewPassword(User user, string newPassword)
{
//a new password hash is generated from a generated salt with the passed settings
user.Password = user.Password = cryptoService.Compute(newPassword, SALT_SIZE, HASH_ITERATIONS);
//assigning the generated salt to the user
user.PasswordSalt = cryptoService.Salt;
@shawnmclean
shawnmclean / basic.cs
Created April 10, 2012 06:26
SimpleCrypto.Net Basic PBKDF2 Hashing
private ICryptoService cryptoService = new PBKDF2();
private void SetNewPassword(User user, string newPassword)
{
//a new password hash is generated from a generated salt with the default settings
user.Password = cryptoService.Compute(newPassword);
//assigning the generated salt to the user
user.PasswordSalt = cryptoService.Salt;
}
@shawnmclean
shawnmclean / getFile.cs
Created April 6, 2012 21:16
Return a file content from asp.net webapi action
public HttpResponseMessage GetText()
{
try
{
string content = "Hello";
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(content);
//a text file is actually an octet-stream (pdf, etc)
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
@shawnmclean
shawnmclean / absolute.cs
Created April 4, 2012 02:52
Absolute Url in Asp.net webapi
//Your ValuesController.cs
//Version 1
string uri = Url.Route("Default", new { controller="Home", action="MyAction", id = 1 });
string absoluteUrl = new Uri(Request.RequestUri, uri).AbsoluteUri;
//http://localhost/Home/MyAction/1
//Version 2
string uri = Url.Route("DefaultApi", new { id = 1 });
string absoluteUrl = new Uri(Request.RequestUri, uri).AbsoluteUri;
@shawnmclean
shawnmclean / activity.js
Created March 3, 2012 20:41
Initializing an Activity.js module
var awayCallback = function(){
console.log(new Date().toTimeString() + ": away");
};
var awayBackCallback = function(){
console.log(new Date().toTimeString() + ": back");
};
var activity = new Activity({
onAway: awayCallback,
@shawnmclean
shawnmclean / gist:1937486
Created February 29, 2012 03:56 — forked from anonymous/gist:1937453
a guest on Feb 28th, 2012 - pastebin.com/5ciDhb6V
myObj = this;
someMethod = function(){
myObj.myProp;
}
@shawnmclean
shawnmclean / img2canvas.html
Created February 8, 2012 01:41
Convert A canvas to image
<h2>Canvas to image</h2>
<canvas id="MyCanvas" width="300" height="300">
</canvas>
<img id="MyImg"/>
<script>
function drawAndConvertStuff(canvas) {
@shawnmclean
shawnmclean / layout.cshtml
Created January 12, 2012 20:01
Using the Custom Identity
Welcome back @(User.Identity.ToCustomIdentity().FirstName) @(User.Identity.ToCustomIdentity().LastName)