Advertisement
uraharadono

Autentifikacija

Jan 19th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Rent_aCar_TravelAgency.Models;
  6. using FEF.Data;
  7. using System.Data.Entity;
  8.  
  9.  
  10. namespace Rent_aCar_TravelAgency.Helper
  11. {
  12.     public class Autentifikacija
  13.     {
  14.         private const string LogiraniKorisnik = "logirani_korisnik";
  15.  
  16.         public static void PokreniNovuSesiju(Korisnik korisnik, HttpContextBase context, bool zapamtiPassword)
  17.         {
  18.             context.Session.Add(LogiraniKorisnik, korisnik);
  19.  
  20.             if (zapamtiPassword)
  21.             {
  22.                 HttpCookie cookie = new HttpCookie("_mvc_session", korisnik != null ? korisnik.Id.ToString() : "");
  23.                 cookie.Expires = DateTime.Now.AddHours(2);
  24.                 context.Response.Cookies.Add(cookie);
  25.             }
  26.         }
  27.  
  28.         public static Korisnik GetLogiraniKorisnik(HttpContextBase context)
  29.         {
  30.             Korisnik korisnik = (Korisnik)context.Session[LogiraniKorisnik];
  31.  
  32.             if (korisnik != null)
  33.                 return korisnik;
  34.  
  35.             HttpCookie cookie = context.Request.Cookies.Get("_mvc_session");
  36.  
  37.             if (cookie == null)
  38.                 return null;
  39.  
  40.             long userId;
  41.             try
  42.             {
  43.                 userId = long.Parse(cookie.Value);
  44.             }
  45.             catch
  46.             {
  47.                 return null;
  48.             }
  49.  
  50.  
  51.  
  52.             using (MojContext db = new MojContext())
  53.             {
  54.                 Korisnik k = db.Korisnici
  55.                     .Include(x => x.Zaposlenik)
  56.                     .Include(x => x.Vozac)
  57.                     .SingleOrDefault(x => x.Id == userId);
  58.  
  59.                 PokreniNovuSesiju(k, context, true);
  60.                 return k;
  61.             }
  62.         }
  63.  
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement