Repository navigation
Expand file tree
/
Copy pathAuthController.cs
More file actions
117 lines (102 loc) · 3.6 KB
/
Copy pathAuthController.cs
File metadata and controls
117 lines (102 loc) · 3.6 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Reflection;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using Opserver.Data.SQL;
using Opserver.Helpers;
using Opserver.Models;
using Opserver.Security;
using Opserver.Views.Login;
namespace Opserver.Controllers
{
public partial class AuthController : StatusController<SQLModule>
{
//private readonly IConfiguration _configuration;
public AuthController(IOptions<OpserverSettings> settings, IHostEnvironment env,SQLModule sqlModule) : base(sqlModule, settings)
{
//_configuration = new ConfigurationBuilder()
//.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
//.AddJsonFile("Config/UserSettings.json", false)
//.Build();
}
[AllowAnonymous]
[Route("login"), HttpGet]
public IActionResult Login(string returnUrl)
{
if (!Current.Security.IsConfigured || Current.Security.FlowType == SecurityProviderFlowType.None)
{
return View("NoConfiguration");
}
if (returnUrl == "/")
{
return RedirectToAction(nameof(Login));
}
return View(new LoginModel());
}
[AllowAnonymous]
[Route("login"), HttpPost]
public async Task<IActionResult> Login(string user, string pass, string url)
{
var returnUrl = url.HasValue() ? url : "~/";
if (Current.Security.FlowType == SecurityProviderFlowType.OIDC)
{
// OpenID Connect needs to go through an authorization flow
// before we can login successfully...
return RedirectToProvider(returnUrl);
}
if (!Current.Security.TryValidateToken(new UserNamePasswordToken(user, pass), out var claimsPrincipal))
{
return View(
"Login",
new LoginModel
{
ErrorMessage = "Login failed, Username or password is incorrect"
}
);
}
if (!ValidateToken(user))
{
return View("Login",
new LoginModel
{
ErrorMessage = "Access denied, You don't have permission to access on this server"
});
}
await HttpContext.SignInAsync(claimsPrincipal);
return Redirect(returnUrl);
}
public bool ValidateToken(string userName)
{
//var query = ReadFromUserSetting().Any(x => x.UserName == userName);
var query = Module.Settings.Users.Any(x => x.UserName == userName);
if (!query)
{
return false;
}
return true;
}
//private List<UserModel> ReadFromUserSetting()
//{
// var users = _configuration.GetSection("users").Get<List<UserModel>>();
// return users;
//}
[Route("logout")]
public async Task<ActionResult> Logout()
{
await HttpContext.SignOutAsync();
return RedirectToAction(nameof(Login));
}
}
}