Skip to content

Instantly share code, notes, and snippets.

View shawnmclean's full-sized avatar
🐭
:)

Shawn Mclean shawnmclean

🐭
:)
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / UserController.cs
Created April 29, 2012 18:59
controller utilizing HttpHeader
[HttpHeader("Access-Control-Allow-Origin", "*")]
public class UserController : ApiController
{
public IEnumerable<string> Get()
{
return new List<string> { "User1", "User2" };
}
}
@shawnmclean
shawnmclean / IRepository.cs
Created August 24, 2012 04:09
Example of a generic repository interface
public interface IRepository<T> where T : class
{
/// <summary>
/// Gets all objects from database
/// </summary>
IQueryable<T> All();
/// <summary>
/// Gets objects from database by filter.
/// </summary>
@shawnmclean
shawnmclean / CameraView.xaml
Created December 10, 2012 16:52
Xaml for full screen camera view
<Grid x:Name="LayoutRoot" VerticalAlignment="Stretch">
<Canvas x:Name="viewfinderCanvas">
<Canvas.Background>
<VideoBrush x:Name="viewfinderBrush">
<VideoBrush.RelativeTransform>
<CompositeTransform x:Name="previewTransform" CenterX=".5" CenterY=".5" />
</VideoBrush.RelativeTransform>
</VideoBrush>
</Canvas.Background>
</Canvas>
@shawnmclean
shawnmclean / CameraView.cs
Created December 11, 2012 14:37
Code behind for full screen camera view
public partial class CameraView : PhoneApplicationPage
{
private PhotoCamera cam;
public CameraView()
{
InitializeComponent();
activateCamera();
}
private void activateCamera()