-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHomeController.cs
More file actions
64 lines (54 loc) · 1.85 KB
/
HomeController.cs
File metadata and controls
64 lines (54 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using Shop.Business.Abstract;
using Shop.Core.CrossCuttingConcerns.Security.Principals;
using Shop.Core.CrossCuttingConcerns.Security.Web;
using Shop.MVCWebUI.Library;
using Shop.MVCWebUI.Library.Attributes;
using Shop.MVCWebUI.Models;
using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
namespace Shop.MVCWebUI.Controllers
{
public class HomeController : Controller
{
private readonly IUserService _userService;
public HomeController(IUserService userService)
{
_userService = userService;
}
[CustomAuthorize(Roles = "Admin,User")]
public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
return View();
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Login(UserLoginViewModel model)
{
var user = _userService.GetByUsernameAndPasswordWithRoles(model.User.Username, model.User.Password);
if (user == null)
{
TempData[Constants.Keys.AlertMessage] = "Login failed!\nUsername or password is incorrect.";
return RedirectToAction("Login", "Home");
}
var serializedModel = new CustomPrincipalSerializedModel
{
Id = user.Id,
FirstName = user.FirstName,
LastName = user.LastName,
Roles = user.Roles.Select(x => x.Name).ToArray()
};
AuthenticationHelper.CreateAuthenticationTicket(user.Username, DateTime.Now.AddMinutes(5), model.RememberMe, serializedModel);
return RedirectToAction("Index", "Home");
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Login");
}
}
}