root/branches/Emisje/Wierszowki/Wierszowki.Web/Controllers/AccountController.cs @ 774

Wersja 752, 2.1 KB (wprowadzona przez marek, 17 years temu)

wiersz

Line 
1using System;
2using System.Web.Mvc;
3using Wierszowki.Core.Interfaces;
4using Wierszowki.Core.Linq;
5using Wierszowki.Models;
6using Wierszowki.Models.Interfaces;
7
8namespace Wierszowki.Controllers
9{
10    public class AccountController : Controller
11    {
12        private readonly IRepository<User> _repository;
13
14        public IFormsAuthentication FormsAuth
15        {
16            get;
17            private set;
18        }
19
20        public AccountController()
21        {
22            _repository = new LinqRepository<User>();
23            FormsAuth = new FormsAuthenticationService();
24        }
25
26        public AccountController(IFormsAuthentication formsAuth, IRepository<User> repository)
27        {
28            _repository = repository;
29            FormsAuth = formsAuth ?? new FormsAuthenticationService();
30        }
31
32        public ActionResult Login()
33        {
34            return View();
35        }
36
37        [AcceptVerbs(HttpVerbs.Post)]
38        public ActionResult Login(string login, string password, bool rememberMe, string returnUrl)
39        {
40            if (!ValidateLogOn(login, password))
41                return View();
42
43            FormsAuth.SignIn(login, rememberMe);
44           
45            if (!String.IsNullOrEmpty(returnUrl))
46                return Redirect(returnUrl);
47           
48            return RedirectToAction("Index", "Home");
49        }
50
51        public ActionResult LogOff()
52        {
53            FormsAuth.SignOut();
54            return RedirectToAction("Index", "Home");
55        }
56
57        private bool ValidateLogOn(string login, string password)
58        {
59            if (string.IsNullOrEmpty(login))
60                ModelState.AddModelError("username", "Proszê podaæ login.");
61            if (string.IsNullOrEmpty(password))
62                ModelState.AddModelError("password", "Proszê podaæ has³o.");
63
64            if (ModelState.IsValid)
65            {
66                if (!_repository.Exists(u => u.Login == login && u.Password == password))
67                    ModelState.AddModelError("_FORM", "Podano nieprawid³owy login lub has³o.");
68            }
69            return ModelState.IsValid;
70        }
71    }
72}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.