Advertisement
Guest User

Untitled

a guest
Sep 12th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using PremiumSMS.Models;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.EntityFrameworkCore;
  7.  
  8. namespace PremiumSMS.Library.Authorization
  9. {
  10.     public class Login
  11.     {
  12.         private string _username;
  13.         private string _password;
  14.         private readonly DatabaseContext _db = new DatabaseContext();
  15.         private HttpContext _context;
  16.  
  17.         public Login(HttpContext  context)
  18.         {
  19.             _context = context;
  20.         }
  21.  
  22.         public bool CreateAuthentication(string username, string password)
  23.         {
  24.             _username = username;
  25.             _password = password;
  26.  
  27.             if (CheckExistsAccount())
  28.             {
  29.                 CreateSessionKey();
  30.                 return true;
  31.             }
  32.             return false;
  33.         }
  34.  
  35.         public bool CheckAuthentication()
  36.         {
  37.             int count = 0;
  38.             if(!string.IsNullOrEmpty(_context.Session.GetString("SessionKey")))
  39.             {
  40.                 count = (from a in _db.Users
  41.                              where a.SessionKey == _context.Session.GetString("SessionKey")
  42.                              select a).Count();
  43.             }
  44.            
  45.             if (count != 0)
  46.             {
  47.                 return true;
  48.             }
  49.             return false;
  50.         }
  51.  
  52.         public void DestroyAuthentication()
  53.         {
  54.             DestroySessionKey();
  55.             _context.Session.Remove("SessionKey");
  56.         }
  57.  
  58.         public void CreateSessionKey()
  59.         {
  60.             string sessionKey = Secure.CreateKey();
  61.  
  62.             var user = (from a in _db.Users
  63.                         where a.Username == _username
  64.                         select a).FirstOrDefault();
  65.  
  66.             if (user != null)
  67.             {
  68.                 user.SessionKey = sessionKey;
  69.  
  70.                 _db.Entry(user).State = EntityState.Modified;
  71.                 _db.SaveChanges();
  72.             }
  73.  
  74.             _context.Session.SetString("SessionKey", sessionKey);
  75.         }
  76.  
  77.         public void DestroySessionKey()
  78.         {
  79.             var user = (from a in _db.Users
  80.                         where a.SessionKey == _context.Session.GetString("SessionKey")
  81.                         select a).FirstOrDefault();
  82.  
  83.             if (user != null)
  84.             {
  85.                 user.SessionKey = null;
  86.  
  87.                 _db.Entry(user).State = EntityState.Modified;
  88.                 _db.SaveChanges();
  89.             }
  90.         }
  91.  
  92.         public bool CheckExistsAccount()
  93.         {
  94.             string securedPassword = Secure.HashString(_password);
  95.  
  96.             var numberAccounts = (from a in _db.Users
  97.                                   where a.Username == _username && a.Password == securedPassword
  98.                                   select a).Count();
  99.  
  100.             if (numberAccounts == 1)
  101.             {
  102.                 return true;
  103.             }
  104.             return false;
  105.         }
  106.  
  107.         public User GetUserObject()
  108.         {
  109.             var user = (from a in _db.Users
  110.                         where a.SessionKey == _context.Session.GetString("SessionKey")
  111.                         select a).FirstOrDefault();
  112.  
  113.             return user;
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement