Authentication added. Login/Logout operational.

This commit is contained in:
2012-12-20 21:39:13 -05:00
parent b30d3068e7
commit f129142dab
24 changed files with 640 additions and 65 deletions
+4 -6
View File
@@ -22,7 +22,7 @@ namespace MileageTraker.Web.Membership
}
using (var context = new MileageTrakerContext())
{
Role role = context.Roles.FirstOrDefault(rl => rl.RoleName == roleName);
var role = context.Roles.FirstOrDefault(rl => rl.RoleName == roleName);
return role != null;
}
}
@@ -39,8 +39,7 @@ namespace MileageTraker.Web.Membership
}
using (var context = new MileageTrakerContext())
{
User user;
user = context.Users.FirstOrDefault(usr => usr.Username == username);
var user = context.Users.FirstOrDefault(usr => usr.Username == username);
if (user == null)
{
return false;
@@ -70,7 +69,7 @@ namespace MileageTraker.Web.Membership
}
using (var context = new MileageTrakerContext())
{
Role role = context.Roles.FirstOrDefault(rl => rl.RoleName == roleName);
var role = context.Roles.FirstOrDefault(rl => rl.RoleName == roleName);
if (role != null)
{
return role.Users.Select(usr => usr.Username).ToArray();
@@ -87,8 +86,7 @@ namespace MileageTraker.Web.Membership
}
using (var context = new MileageTrakerContext())
{
User user;
user = context.Users.FirstOrDefault(Usr => Usr.Username == username);
User user = context.Users.FirstOrDefault(Usr => Usr.Username == username);
if (user != null)
{
return user.Roles.Select(rl => rl.RoleName).ToArray();
+7 -39
View File
@@ -8,24 +8,11 @@ namespace MileageTraker.Web.Membership
{
public static class Crypto
{
private const int TokenSizeInBytes = 16;
private const int Pbkdf2Count = 1000;
private const int Pbkdf2SubkeyLength = 256/8;
private const int SaltSize = 128/8;
public static string GenerateSalt(int byteLength = SaltSize)
{
var Buff = new byte[byteLength];
using (var Prng = new RNGCryptoServiceProvider())
{
Prng.GetBytes(Buff);
}
return Convert.ToBase64String(Buff);
}
public static string Hash(string input, string algorithm = "sha256")
private static string Hash(string input, string algorithm = "sha256")
{
if (input == null)
{
@@ -35,37 +22,24 @@ namespace MileageTraker.Web.Membership
return Hash(Encoding.UTF8.GetBytes(input), algorithm);
}
public static string Hash(byte[] input, string algorithm = "sha256")
private static string Hash(byte[] input, string algorithm = "sha256")
{
if (input == null)
{
throw new ArgumentNullException("input");
}
using (HashAlgorithm alg = HashAlgorithm.Create(algorithm))
using (var alg = HashAlgorithm.Create(algorithm))
{
if (alg != null)
{
byte[] hashData = alg.ComputeHash(input);
var hashData = alg.ComputeHash(input);
return BinaryToHex(hashData);
}
else
{
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "not supported hash alg", algorithm));
}
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "not supported hash alg", algorithm));
}
}
public static string SHA1(string input)
{
return Hash(input, "sha1");
}
public static string SHA256(string input)
{
return Hash(input, "sha256");
}
/* =======================
* HASHED PASSWORD FORMATS
* =======================
@@ -79,9 +53,7 @@ namespace MileageTraker.Web.Membership
public static string HashPassword(string password)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
byte[] salt;
byte[] subkey;
@@ -101,15 +73,11 @@ namespace MileageTraker.Web.Membership
public static bool VerifyHashedPassword(string hashedPassword, string password)
{
if (hashedPassword == null)
{
throw new ArgumentNullException("hashedPassword");
}
if (password == null)
{
throw new ArgumentNullException("password");
}
byte[] hashedPasswordBytes = Convert.FromBase64String(hashedPassword);
var hashedPasswordBytes = Convert.FromBase64String(hashedPassword);
// Verify a version 0 (see comment above) password hash.
@@ -132,7 +100,7 @@ namespace MileageTraker.Web.Membership
return ByteArraysEqual(storedSubkey, generatedSubkey);
}
internal static string BinaryToHex(byte[] data)
private static string BinaryToHex(byte[] data)
{
var hex = new char[data.Length*2];
+10 -19
View File
@@ -9,7 +9,7 @@ using MileageTraker.Web.Context;
namespace MileageTraker.Web.Membership
{
public sealed class WebSecurity
public static class WebSecurity
{
public static HttpContextBase Context
{
@@ -121,17 +121,17 @@ namespace MileageTraker.Web.Membership
public static string CreateUserAndAccount(string userName, string password)
{
return CreateUserAndAccount(userName, password, propertyValues: null, requireConfirmationToken: false);
return CreateUserAndAccount(userName, password, propertyValues: null);
}
public static string CreateUserAndAccount(string userName, string password, bool requireConfirmation)
{
return CreateUserAndAccount(userName, password, propertyValues: null, requireConfirmationToken: requireConfirmation);
return CreateUserAndAccount(userName, password, null, requireConfirmation);
}
public static string CreateUserAndAccount(string userName, string password, IDictionary<string, object> values)
{
return CreateUserAndAccount(userName, password, propertyValues: values, requireConfirmationToken: false);
return CreateUserAndAccount(userName, password, values, requireConfirmationToken: false);
}
public static string CreateUserAndAccount(string userName, string password, object propertyValues = null,
@@ -148,39 +148,30 @@ namespace MileageTraker.Web.Membership
return codeFirstMembership.CreateUserAndAccount(userName, password, requireConfirmationToken, values);
}
public static List<MembershipUser> FindUsersByEmail(string Email, int PageIndex, int PageSize)
public static List<MembershipUser> FindUsersByEmail(string email, int pageIndex, int pageSize)
{
int totalRecords;
return
System.Web.Security.Membership.FindUsersByEmail(Email, PageIndex, PageSize, out totalRecords)
System.Web.Security.Membership.FindUsersByEmail(email, pageIndex, pageSize, out totalRecords)
.Cast<MembershipUser>()
.ToList();
}
public static List<MembershipUser> FindUsersByName(string Username, int PageIndex, int PageSize)
public static List<MembershipUser> FindUsersByName(string username, int pageIndex, int pageSize)
{
int totalRecords;
return
System.Web.Security.Membership.FindUsersByName(Username, PageIndex, PageSize, out totalRecords)
System.Web.Security.Membership.FindUsersByName(username, pageIndex, pageSize, out totalRecords)
.Cast<MembershipUser>()
.ToList();
}
public static List<MembershipUser> GetAllUsers(int PageIndex, int PageSize)
public static List<MembershipUser> GetAllUsers(int pageIndex, int pageSize)
{
int totalRecords;
return
System.Web.Security.Membership.GetAllUsers(PageIndex, PageSize, out totalRecords).Cast<MembershipUser>().ToList();
System.Web.Security.Membership.GetAllUsers(pageIndex, pageSize, out totalRecords).Cast<MembershipUser>().ToList();
}
public static void InitializeDatabaseConnection(string connectionStringName, string userTableName, string userIdColumn,
string userNameColumn, bool autoCreateTables)
{
}
public static void InitializeDatabaseConnection(string connectionString, string providerName, string userTableName,
string userIdColumn, string userNameColumn, bool autoCreateTables)
{
}
}
}