fix login issue

strip colors for logging
feature implementation for issue #76
This commit is contained in:
RaidMax 2019-08-04 20:38:55 -05:00
parent 75378400e7
commit 08d250156c
6 changed files with 52 additions and 18 deletions

View File

@ -60,6 +60,7 @@ namespace IW4MAdmin.Application
await OnLogWriting.WaitAsync(); await OnLogWriting.WaitAsync();
string stringType = type.ToString(); string stringType = type.ToString();
msg = msg.StripColors();
try try
{ {
@ -73,13 +74,13 @@ namespace IW4MAdmin.Application
{ {
#if DEBUG #if DEBUG
// lets keep it simple and dispose of everything quickly as logging wont be that much (relatively) // lets keep it simple and dispose of everything quickly as logging wont be that much (relatively)
Console.WriteLine(LogLine.StripColors()); Console.WriteLine(LogLine);
await File.AppendAllTextAsync(FileName, $"{LogLine}{Environment.NewLine}"); await File.AppendAllTextAsync(FileName, $"{LogLine}{Environment.NewLine}");
//Debug.WriteLine(msg); //Debug.WriteLine(msg);
#else #else
if (type == LogType.Error || type == LogType.Verbose) if (type == LogType.Error || type == LogType.Verbose)
{ {
Console.WriteLine(LogLine.StripColors()); Console.WriteLine(LogLine);
} }
await File.AppendAllTextAsync(FileName, $"{LogLine}{Environment.NewLine}"); await File.AppendAllTextAsync(FileName, $"{LogLine}{Environment.NewLine}");
#endif #endif

View File

@ -156,6 +156,14 @@ namespace SharedLibraryCore
/// <returns></returns> /// <returns></returns>
public static string FixIW4ForwardSlash(this string str) => str.Replace("/", " /"); public static string FixIW4ForwardSlash(this string str) => str.Replace("/", " /");
private static readonly IList<string> _zmGameTypes = new[] { "zclassic", "zstandard", "zcleansed", "zgrief" };
/// <summary>
/// indicates if the given server is running a zombie game mode
/// </summary>
/// <param name="server"></param>
/// <returns></returns>
public static bool IsZombieServer(this Server server) => server.GameName == Game.T6 && _zmGameTypes.Contains(server.Gametype.ToLower());
/// <summary> /// <summary>
/// Get the IW Engine color code corresponding to an admin level /// Get the IW Engine color code corresponding to an admin level
/// </summary> /// </summary>

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SharedLibraryCore; using SharedLibraryCore;
using System; using System;
@ -10,13 +11,9 @@ namespace WebfrontCore.Controllers
{ {
public class AccountController : BaseController public class AccountController : BaseController
{ {
/// <summary>
/// life span in months
/// </summary>
private const int COOKIE_LIFESPAN = 3;
[HttpGet] [HttpGet]
public async Task<IActionResult> LoginAsync(int clientId, string password, Microsoft.AspNetCore.Http.HttpContext ctx = null) public async Task<IActionResult> LoginAsync(int clientId, string password)
{ {
if (clientId == 0 || string.IsNullOrEmpty(password)) if (clientId == 0 || string.IsNullOrEmpty(password))
{ {
@ -48,13 +45,7 @@ namespace WebfrontCore.Controllers
var claimsIdentity = new ClaimsIdentity(claims, "login"); var claimsIdentity = new ClaimsIdentity(claims, "login");
var claimsPrinciple = new ClaimsPrincipal(claimsIdentity); var claimsPrinciple = new ClaimsPrincipal(claimsIdentity);
await (ctx ?? HttpContext).SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrinciple, new AuthenticationProperties() await SignInAsync(claimsPrinciple);
{
AllowRefresh = true,
ExpiresUtc = DateTime.UtcNow.AddMonths(COOKIE_LIFESPAN),
IsPersistent = true,
IssuedUtc = DateTime.UtcNow
});
return Ok(); return Ok();
} }

View File

@ -238,6 +238,15 @@ namespace WebfrontCore.Controllers
{ {
var server = Manager.GetServers().First(_server => _server.EndPoint == id); var server = Manager.GetServers().First(_server => _server.EndPoint == id);
server.ChatHistory.Add(new SharedLibraryCore.Dtos.ChatInfo()
{
ClientId = Client.ClientId,
Message = message,
Name = Client.Name,
ServerGame = server.GameName,
Time = DateTime.Now
});
return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new return await Task.FromResult(RedirectToAction("ExecuteAsync", "Console", new
{ {
serverId = server.EndPoint, serverId = server.EndPoint,

View File

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using SharedLibraryCore; using SharedLibraryCore;
@ -9,12 +11,18 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Threading.Tasks;
using WebfrontCore.ViewModels; using WebfrontCore.ViewModels;
namespace WebfrontCore.Controllers namespace WebfrontCore.Controllers
{ {
public class BaseController : Controller public class BaseController : Controller
{ {
/// <summary>
/// life span in months
/// </summary>
private const int COOKIE_LIFESPAN = 3;
protected IManager Manager; protected IManager Manager;
protected readonly DatabaseContext Context; protected readonly DatabaseContext Context;
protected bool Authorized { get; set; } protected bool Authorized { get; set; }
@ -55,6 +63,17 @@ namespace WebfrontCore.Controllers
ViewBag.EnableColorCodes = Manager.GetApplicationSettings().Configuration().EnableColorCodes; ViewBag.EnableColorCodes = Manager.GetApplicationSettings().Configuration().EnableColorCodes;
} }
protected async Task SignInAsync(ClaimsPrincipal claimsPrinciple)
{
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrinciple, new AuthenticationProperties()
{
AllowRefresh = true,
ExpiresUtc = DateTime.UtcNow.AddMonths(COOKIE_LIFESPAN),
IsPersistent = true,
IssuedUtc = DateTime.UtcNow
});
}
public override void OnActionExecuting(ActionExecutingContext context) public override void OnActionExecuting(ActionExecutingContext context)
{ {
Client = Client ?? new EFClient() Client = Client ?? new EFClient()
@ -99,11 +118,15 @@ namespace WebfrontCore.Controllers
Client.Level = EFClient.Permission.Console; Client.Level = EFClient.Permission.Console;
Client.CurrentAlias = new EFAlias() { Name = "IW4MAdmin" }; Client.CurrentAlias = new EFAlias() { Name = "IW4MAdmin" };
Authorized = true; Authorized = true;
using (var controller = new AccountController()) var claims = new[]
{ {
_ = controller.LoginAsync(1, "password", HttpContext).Result; new Claim(ClaimTypes.NameIdentifier, Client.CurrentAlias.Name),
} new Claim(ClaimTypes.Role, Client.Level.ToString()),
new Claim(ClaimTypes.Sid, Client.ClientId.ToString()),
new Claim(ClaimTypes.PrimarySid, Client.NetworkId.ToString("X"))
};
var claimsIdentity = new ClaimsIdentity(claims, "login");
SignInAsync(new ClaimsPrincipal(claimsIdentity)).Wait();
} }
ViewBag.Authorized = Authorized; ViewBag.Authorized = Authorized;

View File

@ -73,6 +73,8 @@ namespace WebfrontCore
mvcBuilder.AddApplicationPart(asm); mvcBuilder.AddApplicationPart(asm);
} }
services.AddHttpContextAccessor();
services.AddEntityFrameworkSqlite() services.AddEntityFrameworkSqlite()
.AddDbContext<DatabaseContext>(); .AddDbContext<DatabaseContext>();