Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Objects;
- using System.Data.Linq;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- namespace ScorBotRobotics.Repository
- {
- public interface IUserRepository : IRepository<User>
- {
- /// <summary>
- /// Retrieves a User by his username and matching password.
- /// </summary>
- /// <param name="username">The User's username.</param>
- /// <param name="password">The User's password.</param>
- /// <returns>Returns the first matching user, or a default value if no user is found.</returns>
- User GetUserByNameAndPassword(string username, string password);
- }
- public class UserRepository : IUserRepository
- {
- private ScorBotDataContext context;
- public UserRepository()
- : this(new ScorBotDataContext())
- {
- }
- public UserRepository(ScorBotDataContext context)
- {
- this.context = context;
- }
- public Binary ComputeHash(string password)
- {
- using (var sha1 = SHA1Managed.Create())
- {
- return sha1.ComputeHash(Encoding.Default.GetBytes(password));
- }
- }
- #region IUserRepository Members
- /// <summary>
- /// Retrieves a User by his username and matching password.
- /// </summary>
- /// <param name="username">The User's username.</param>
- /// <param name="password">The User's password.</param>
- /// <returns>Returns the first matching user, or a default value if no user is found.</returns>
- public User GetUserByNameAndPassword(string username, string password)
- {
- var result =
- from user in context.Users
- where user.Username == username
- where user.Password == ComputeHash(password)
- select user;
- return result.FirstOrDefault();
- }
- #endregion
- #region IRepository<User> Members
- public User GetById(int id)
- {
- return context.Users.FirstOrDefault(u => u.UserId == id);
- }
- public IQueryable<User> GetAll()
- {
- return context.Users;
- }
- public void InsertOnSubmit(User entity)
- {
- context.Users.InsertOnSubmit(entity);
- }
- public void DeleteOnSubmit(User entity)
- {
- context.Users.DeleteOnSubmit(entity);
- }
- public void SubmitChanges()
- {
- context.SubmitChanges();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement