Removed PdfViewController, HtmlViewRenderer and FakeView to be replaced with ViewEngineResult Render method

Added HttpException class for missing HttpException for net8

Wrapped HttpContext.Session, GetJsonResult, IsAjaxRequest and GetUserIdentityName in controllers for net8

Added AuthenticationService to test Fab2ApprovalMKLink code for net8

Compile conditionally flags to debug in dotnet core
This commit is contained in:
2025-05-23 12:51:42 -07:00
parent 184e97fce3
commit 7650bf2869
85 changed files with 3655 additions and 1259 deletions

View File

@ -1,30 +1,47 @@
#if !NET8
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
#endif
#if NET8
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
#endif
#if !NET8
using Fab2ApprovalSystem.DMO;
using Fab2ApprovalSystem.Misc;
using Fab2ApprovalSystem.Models;
#endif
#if !NET8
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
#endif
#if !NET8
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Fab2ApprovalSystem.DMO;
using Fab2ApprovalSystem.Misc;
using Fab2ApprovalSystem.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using Newtonsoft.Json;
#endif
namespace Fab2ApprovalSystem.Controllers;
[Authorize]
#if NET8
[Route("[controller]")]
#endif
public class AccountController : Controller {
#if !NET8
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) {
}
@ -35,25 +52,23 @@ public class AccountController : Controller {
public UserManager<ApplicationUser> UserManager { get; private set; }
#endif
#if !NET8
// GET: /Account/Login
[AllowAnonymous]
// try to make the browser refresh the login page every time, to prevent issues with changing usernames and the anti-forgery token validation
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
#endif
public ActionResult Login(string returnUrl) {
ViewBag.ReturnUrl = returnUrl;
return View();
}
private void SetSessionParameters(LoginResult loginResult, LoginModel user) {
Session["JWT"] = loginResult.AuthTokens.JwtToken;
Session["RefreshToken"] = loginResult.AuthTokens.RefreshToken;
#if !NET8
Session[GlobalVars.SESSION_USERID] = user.UserID;
Session[GlobalVars.SESSION_USERNAME] = user.FullName;
Session[GlobalVars.IS_ADMIN] = user.IsAdmin;
Session[GlobalVars.IS_MANAGER] = user.IsManager;
Session[GlobalVars.OOO] = user.OOO;
Session[GlobalVars.CAN_CREATE_PARTS_REQUEST] = user.IsAdmin || PartsRequestController.CanCreatePartsRequest(user.UserID);
private void SetSessionParameters(LoginResult loginResult, LoginModel user) {
GlobalVars.SetSessionParameters(GetSession(), loginResult, user);
FormsAuthentication.SetAuthCookie(user.LoginID, true);
}
@ -102,8 +117,8 @@ public class AccountController : Controller {
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
} catch (Exception ex) {
Functions.WriteEvent(GlobalVars.AppSettings, @User.Identity.Name + " " + ex.InnerException, System.Diagnostics.EventLogEntryType.Error);
EventLogDMO.Add(new WinEventLog() { IssueID = 99999, UserID = @User.Identity.Name, DocumentType = "Login", OperationType = "Error", Comments = "Reject - " + ex.Message });
Functions.WriteEvent(GlobalVars.AppSettings, GetUserIdentityName() + " " + ex.InnerException, System.Diagnostics.EventLogEntryType.Error);
EventLogDMO.Add(new WinEventLog() { IssueID = 99999, UserID = GetUserIdentityName(), DocumentType = "Login", OperationType = "Error", Comments = "Reject - " + ex.Message });
ModelState.AddModelError("", ex.Message);
}
@ -158,20 +173,24 @@ public class AccountController : Controller {
return new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
} catch (Exception ex) {
Functions.WriteEvent(GlobalVars.AppSettings, @User.Identity.Name + " " + ex.InnerException, System.Diagnostics.EventLogEntryType.Error);
EventLogDMO.Add(new WinEventLog() { IssueID = 99999, UserID = @User.Identity.Name, DocumentType = "Login", OperationType = "Error", Comments = "Reject - " + ex.Message });
Functions.WriteEvent(GlobalVars.AppSettings, GetUserIdentityName() + " " + ex.InnerException, System.Diagnostics.EventLogEntryType.Error);
EventLogDMO.Add(new WinEventLog() { IssueID = 99999, UserID = GetUserIdentityName(), DocumentType = "Login", OperationType = "Error", Comments = "Reject - " + ex.Message });
ModelState.AddModelError("", ex.Message);
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
#endif
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register() {
return View();
}
#if !NET8
// POST: /Account/Disassociate
[HttpPost]
[ValidateAntiForgeryToken]
@ -186,6 +205,8 @@ public class AccountController : Controller {
return RedirectToAction("Manage", new { Message = message });
}
#endif
// GET: /Account/Manage
#pragma warning disable IDE0060 // Remove unused parameter
public ActionResult Manage(ManageMessageId? message) {
@ -193,6 +214,8 @@ public class AccountController : Controller {
}
#pragma warning restore IDE0060 // Remove unused parameter
#if !NET8
// POST: /Account/ExternalLogin
[HttpPost]
[AllowAnonymous]
@ -223,7 +246,6 @@ public class AccountController : Controller {
return RedirectToAction("Manage", new { Message = ManageMessageId.Error });
}
// POST: /Account/LogOff
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff() {
@ -231,12 +253,15 @@ public class AccountController : Controller {
return RedirectToAction("Login", "Account");
}
// GET: /Account/ExternalLoginFailure
#endif
[AllowAnonymous]
public ActionResult ExternalLoginFailure() {
return View();
}
#if !NET8
[ChildActionOnly]
public ActionResult RemoveAccountList() {
IList<UserLoginInfo> linkedAccounts = UserManager.GetLogins(User.Identity.GetUserId());
@ -252,10 +277,14 @@ public class AccountController : Controller {
base.Dispose(disposing);
}
#endif
#region Helpers
// Used for XSRF protection when adding external logins
private const string XsrfKey = "XsrfId";
#if !NET8
private IAuthenticationManager AuthenticationManager {
get {
return HttpContext.GetOwinContext().Authentication;
@ -282,6 +311,8 @@ public class AccountController : Controller {
return false;
}
#endif
public enum ManageMessageId {
ChangePasswordSuccess,
SetPasswordSuccess,
@ -297,7 +328,18 @@ public class AccountController : Controller {
}
}
#if !NET8
private class ChallengeResult : HttpUnauthorizedResult {
#endif
#if NET8
private class ChallengeResult {
#endif
public ChallengeResult(string provider, string redirectUri) : this(provider, redirectUri, null) {
}
@ -311,6 +353,8 @@ public class AccountController : Controller {
public string RedirectUri { get; set; }
public string UserId { get; set; }
#if !NET8
public override void ExecuteResult(ControllerContext context) {
AuthenticationProperties properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
if (UserId != null) {
@ -318,6 +362,40 @@ public class AccountController : Controller {
}
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
}
#endif
}
#endregion
#if !NET8
private System.Web.HttpSessionStateBase GetSession() =>
Session;
private JsonResult GetJsonResult(object? data) =>
Json(data, JsonRequestBehavior.AllowGet);
private bool IsAjaxRequest() =>
Request.IsAjaxRequest();
#endif
#if NET8
private Microsoft.AspNetCore.Http.ISession GetSession() =>
HttpContext.Session;
private JsonResult GetJsonResult(object? data) =>
Json(data);
private bool IsAjaxRequest() =>
Request.Headers.TryGetValue("X-Requested-With", out Microsoft.Extensions.Primitives.StringValues strings) && strings[0] == "XMLHttpRequest";
#endif
private string GetUserIdentityName() =>
@User.Identity.Name;
}