Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Objects;
  5. using System.Data.Linq;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9.  
  10. namespace ScorBotRobotics.Repository
  11. {
  12.     public interface IUserRepository : IRepository<User>
  13.     {
  14.         /// <summary>
  15.         /// Retrieves a User by his username and matching password.
  16.         /// </summary>
  17.         /// <param name="username">The User's username.</param>
  18.         /// <param name="password">The User's password.</param>
  19.         /// <returns>Returns the first matching user, or a default value if no user is found.</returns>
  20.         User GetUserByNameAndPassword(string username, string password);
  21.     }
  22.  
  23.     public class UserRepository : IUserRepository
  24.     {
  25.         private ScorBotDataContext context;
  26.  
  27.         public UserRepository()
  28.             : this(new ScorBotDataContext())
  29.         {
  30.         }
  31.  
  32.         public UserRepository(ScorBotDataContext context)
  33.         {
  34.             this.context = context;
  35.         }
  36.  
  37.         public Binary ComputeHash(string password)
  38.         {
  39.             using (var sha1 = SHA1Managed.Create())
  40.             {
  41.                 return sha1.ComputeHash(Encoding.Default.GetBytes(password));
  42.             }
  43.         }
  44.  
  45.         #region IUserRepository Members
  46.  
  47.         /// <summary>
  48.         /// Retrieves a User by his username and matching password.
  49.         /// </summary>
  50.         /// <param name="username">The User's username.</param>
  51.         /// <param name="password">The User's password.</param>
  52.         /// <returns>Returns the first matching user, or a default value if no user is found.</returns>
  53.         public User GetUserByNameAndPassword(string username, string password)
  54.         {
  55.             var result =
  56.                 from user in context.Users
  57.                 where user.Username == username
  58.                 where user.Password == ComputeHash(password)
  59.                 select user;
  60.  
  61.             return result.FirstOrDefault();
  62.         }
  63.  
  64.         #endregion
  65.  
  66.         #region IRepository<User> Members
  67.  
  68.         public User GetById(int id)
  69.         {
  70.             return context.Users.FirstOrDefault(u => u.UserId == id);
  71.         }
  72.  
  73.         public IQueryable<User> GetAll()
  74.         {
  75.             return context.Users;
  76.         }
  77.  
  78.         public void InsertOnSubmit(User entity)
  79.         {
  80.             context.Users.InsertOnSubmit(entity);
  81.         }
  82.  
  83.         public void DeleteOnSubmit(User entity)
  84.         {
  85.             context.Users.DeleteOnSubmit(entity);
  86.         }
  87.  
  88.         public void SubmitChanges()
  89.         {
  90.             context.SubmitChanges();
  91.         }
  92.  
  93.         #endregion
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement