-
Notifications
You must be signed in to change notification settings - Fork 10
/
HomeController.cs
69 lines (62 loc) · 2.39 KB
/
HomeController.cs
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
65
66
67
68
69
using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
namespace Watmwithauth.Controllers
{
public class HomeController : Controller
{
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(string username, string password, string returnUrl)
{
if (((username?.ToLowerInvariant() == "admin") || (username?.ToLowerInvariant() == "superadmin") || (username?.ToLowerInvariant() == "specialadmin")) && (password?.ToLowerInvariant() == "admin"))
{
var claimsIdentity = new ClaimsIdentity(
new[] {
new Claim(ClaimTypes.Name,username),
},
CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
AllowRefresh = true,
ExpiresUtc = DateTimeOffset.Now.AddDays(1),
IsPersistent = true,
};
if (username?.ToLowerInvariant() == "superadmin")
{
claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, "superadmin"));
}
if (username?.ToLowerInvariant() == "specialadmin")
{
claimsIdentity.AddClaim(new Claim(ClaimTypes.UserData, "IsSpecialAdmin"));
}
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);
if (string.IsNullOrEmpty(returnUrl))
{
return RedirectToAction("Login", "Home");
}
return Redirect(returnUrl);
}
else
{
return View();
}
}
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync();
return RedirectToAction("Login", "Home");
}
public IActionResult AccessDenied(string returnUrl)
{
ViewData["ReturnUrl"] = returnUrl;
return View();
}
}
}