1 | using Microsoft.AspNetCore.Authentication.Cookies;
|
---|
2 | using Microsoft.AspNetCore.Authentication;
|
---|
3 | using Microsoft.AspNetCore.Mvc;
|
---|
4 | using System.Security.Claims;
|
---|
5 | using System.Diagnostics;
|
---|
6 | using Dal.ApplicationStorage;
|
---|
7 | using Models.DatabaseModels;
|
---|
8 | using Dal.ApplicationStorage.DataAccess.Abstract;
|
---|
9 | using Models.DataTransferObjects;
|
---|
10 | using System.Text.RegularExpressions;
|
---|
11 |
|
---|
12 | namespace ocrent.Controllers
|
---|
13 | {
|
---|
14 | public class IdentityController : Controller
|
---|
15 | {
|
---|
16 | private readonly IIdentityCustomDa _identityDa;
|
---|
17 | private List<Claim> claims;
|
---|
18 |
|
---|
19 | public IdentityController(IIdentityCustomDa identityDa)
|
---|
20 | {
|
---|
21 | _identityDa = identityDa;
|
---|
22 | claims = new List<Claim>();
|
---|
23 | }
|
---|
24 |
|
---|
25 | public IActionResult Index()
|
---|
26 | {
|
---|
27 | return View();
|
---|
28 | }
|
---|
29 |
|
---|
30 | public async Task<IActionResult> Register()
|
---|
31 | {
|
---|
32 | return View();
|
---|
33 | }
|
---|
34 |
|
---|
35 | [HttpPost]
|
---|
36 | [ValidateAntiForgeryToken]
|
---|
37 | public async Task<IActionResult> Register(RegisterDTO registerInfo)
|
---|
38 | {
|
---|
39 | if (ModelState.IsValid)
|
---|
40 | {
|
---|
41 | if (registerInfo.Pass.Equals(registerInfo.ConfirmPass))
|
---|
42 | {
|
---|
43 | await _identityDa.Register(registerInfo);
|
---|
44 | return RedirectToAction("Login", "Identity");
|
---|
45 | }
|
---|
46 | else
|
---|
47 | {
|
---|
48 | ModelState.AddModelError("ConfirmPass", "Password does not match!");
|
---|
49 | return View("Register");
|
---|
50 | }
|
---|
51 |
|
---|
52 | }
|
---|
53 | return View("Register");
|
---|
54 | }
|
---|
55 |
|
---|
56 | public async Task<IActionResult> Login()
|
---|
57 | {
|
---|
58 | return View();
|
---|
59 | }
|
---|
60 |
|
---|
61 | [HttpPost]
|
---|
62 | public async Task<ActionResult> Login(LoginDTO model)
|
---|
63 | {
|
---|
64 | if (ModelState.IsValid)
|
---|
65 | {
|
---|
66 | var userdetails = await _identityDa.CheckLoginInformation(model);
|
---|
67 | if (!userdetails.ValidPassword || !userdetails.ValidEmail)
|
---|
68 | {
|
---|
69 | if (!userdetails.ValidEmail)
|
---|
70 | ModelState.AddModelError("Email", "There is no user with this email");
|
---|
71 | else
|
---|
72 | ModelState.AddModelError("Password", "Wrong password.");
|
---|
73 |
|
---|
74 | return View("Login");
|
---|
75 | }
|
---|
76 | HttpContext.Session.SetString("userId", userdetails.UserId.ToString());
|
---|
77 | HttpContext.Session.SetString("email", userdetails.Email);
|
---|
78 | HttpContext.Session.SetString("firstname", userdetails.FirstName);
|
---|
79 |
|
---|
80 | await AddClaimAsync(model.Email, model.Claim);
|
---|
81 |
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | return View("Login");
|
---|
86 | }
|
---|
87 | return RedirectToAction("Index", "Home");
|
---|
88 | }
|
---|
89 |
|
---|
90 | public IActionResult TestClaimReading()
|
---|
91 | {
|
---|
92 | var email = HttpContext.Session.GetString("email");
|
---|
93 | var customClaim = HttpContext.User.FindFirst(email);
|
---|
94 |
|
---|
95 | return Content($"Claim value: {customClaim.Value}");
|
---|
96 | }
|
---|
97 |
|
---|
98 | private async Task AddClaimAsync(string email, string claimType)
|
---|
99 | {
|
---|
100 | Claim claim = new Claim(email, claimType);
|
---|
101 | claims.Add(claim);
|
---|
102 |
|
---|
103 | var claimsIdentity = new ClaimsIdentity(
|
---|
104 | claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
---|
105 |
|
---|
106 | await HttpContext.SignInAsync(
|
---|
107 | CookieAuthenticationDefaults.AuthenticationScheme,
|
---|
108 | new ClaimsPrincipal(claimsIdentity));
|
---|
109 | }
|
---|
110 |
|
---|
111 | public IActionResult Logout()
|
---|
112 | {
|
---|
113 | HttpContext.Session.Clear();
|
---|
114 | return RedirectToAction("Index", "Home");
|
---|
115 | }
|
---|
116 |
|
---|
117 | public void ValidationMessage(string key, string alert, string value)
|
---|
118 | {
|
---|
119 | try
|
---|
120 | {
|
---|
121 | TempData.Remove(key);
|
---|
122 | TempData.Add(key, value);
|
---|
123 | TempData.Add("alertType", alert);
|
---|
124 | }
|
---|
125 | catch
|
---|
126 | {
|
---|
127 | Debug.WriteLine("TempDataMessage Error");
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | //private bool ValidateLogin(string username, string password)
|
---|
133 | //{
|
---|
134 | // // Check the user/pass here!
|
---|
135 | // // For example's sake, the credentials are always valid.
|
---|
136 | // return true;
|
---|
137 | //}
|
---|
138 |
|
---|
139 | //private async Task SignInUser(string username)
|
---|
140 | //{
|
---|
141 | // var claims = new List<Claim>
|
---|
142 | // {
|
---|
143 | // new Claim(ClaimTypes.Name, username),
|
---|
144 | // new Claim("MyCustomClaim", "my claim value")
|
---|
145 | // };
|
---|
146 |
|
---|
147 | // var claimsIdentity = new ClaimsIdentity(
|
---|
148 | // claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
---|
149 |
|
---|
150 | // await HttpContext.SignInAsync(
|
---|
151 | // CookieAuthenticationDefaults.AuthenticationScheme,
|
---|
152 | // new ClaimsPrincipal(claimsIdentity));
|
---|
153 | //}
|
---|
154 | }
|
---|
155 | }
|
---|