77 lines
2.7 KiB
C#
77 lines
2.7 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using System.Security.Claims;
|
|
using MistoxWebsite.Server.Entities;
|
|
using MistoxWebsite.Server.Services.DatabaseService;
|
|
|
|
namespace MistoxWebsite.Server.Controllers {
|
|
|
|
public class MistoxControllerBase : ControllerBase {
|
|
|
|
public DatabaseService _databaseService;
|
|
|
|
public MistoxControllerBase(DatabaseService databaseService) {
|
|
_databaseService = databaseService;
|
|
}
|
|
|
|
public void signIn(string JWT) {
|
|
Response.Cookies.Append("mistox_session", JWT, new CookieOptions {
|
|
Secure = true,
|
|
HttpOnly = true,
|
|
SameSite = SameSiteMode.Strict,
|
|
Expires = DateTime.UtcNow.AddDays(7)
|
|
});
|
|
}
|
|
|
|
public void signOut() {
|
|
Response.Cookies.Delete("mistox_session");
|
|
}
|
|
|
|
public bool isLoggedIn() {
|
|
if (User.Identity != null && User.Identity.IsAuthenticated) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int getLoggedInUserID() {
|
|
return Convert.ToInt32(User.FindFirstValue(ClaimTypes.NameIdentifier));
|
|
}
|
|
|
|
public Account getLoggedInUser() {
|
|
try {
|
|
Account building = new Account {
|
|
ID = Convert.ToInt32(User.FindFirstValue(ClaimTypes.NameIdentifier)),
|
|
UserName = User.FindFirstValue(ClaimTypes.Name)!.ToString(),
|
|
Email = User.FindFirstValue(ClaimTypes.Email)!.ToString(),
|
|
Role = User.FindFirstValue(ClaimTypes.Role)!.ToString(),
|
|
DataServer = User.FindFirstValue(ClaimTypes.UserData)!.ToString()
|
|
};
|
|
return building;
|
|
} catch {
|
|
return new Account();
|
|
}
|
|
}
|
|
|
|
public string Substitue(string message, string subString, string Replacement) {
|
|
for (int i = 0; i < (message.Length - subString.Length); i++) {
|
|
if (message.Substring(i, subString.Length) == subString) {
|
|
string before = message.Substring(0, i);
|
|
string after = message.Substring(i + subString.Length);
|
|
return before + Replacement + after;
|
|
}
|
|
}
|
|
return message;
|
|
}
|
|
|
|
public bool contains(string outer, string inner) {
|
|
if (outer.Length >= inner.Length) {
|
|
for (int i = 0; i < outer.Length - inner.Length; i++) {
|
|
if (outer.Substring(i, inner.Length) == inner) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
} |