Created
August 14, 2010 02:04
-
-
Save mikeobrien/523856 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Security.Cryptography; | |
using System.Text; | |
using Norm; | |
using System.Linq; | |
namespace MongoSandbox | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
const string connectionString = | |
"mongodb://projecteuleruser:Pa$$w0rd@localhost/ProjectEuler"; | |
using (var database = Mongo.Create(connectionString)) | |
{ | |
var users = database.GetCollection<User>(); | |
users.Find().ForEach(users.Delete); | |
users.Save(new User | |
{ | |
Username = "ldebroglie", | |
Email = "[email protected]", | |
Name = "Louis De Broglie", | |
Password = "wavemechanics".Hash("ldebroglie") | |
}); | |
users.Save(new User | |
{ | |
Username = "wheisenberg", | |
Email = "[email protected]", | |
Name = "Werner Heisenberg", | |
Password = "matrixmechanics".Hash("wheisenberg") | |
}); | |
users.Find().ForEach(Console.WriteLine); | |
Console.WriteLine("Authenticated: {0}", | |
(users.AsQueryable(). | |
Any(u => u.Username == "wheisenberg" && | |
u.Password == "wavemechanics".Hash("wheisenberg")))); | |
} | |
Console.ReadKey(); | |
} | |
} | |
public class User | |
{ | |
public User() { Id = Guid.NewGuid(); } | |
public Guid Id { get; set; } | |
public string Username { get; set; } | |
public string Password { get; set; } | |
public string Name { get; set; } | |
public string Email { get; set; } | |
public override string ToString() | |
{ | |
return string.Format("Username: {0}, Password: {1}, Name: {2}, Email: {3}", | |
Username, | |
Password, | |
Name, | |
Email); | |
} | |
} | |
public static class StringExtensions | |
{ | |
public static string Hash(this string value, string salt) | |
{ | |
return SHA1.Create(). | |
ComputeHash(Encoding.UTF8.GetBytes(salt + value)). | |
Select(x => x.ToString("x2")). | |
Aggregate((a, x) => a + x); | |
} | |
} | |
public static class IEnumerableExtensions | |
{ | |
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action) | |
{ | |
foreach (var item in items) action(item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment