Skip to content

Commit

Permalink
Merge pull request #218 from Blazam-App/Beta-Dev
Browse files Browse the repository at this point in the history
Fix for impersonation not impersonating
  • Loading branch information
jacobsen9026 authored Feb 11, 2024
2 parents 6b89ae9 + 91d44f3 commit 4edb82d
Show file tree
Hide file tree
Showing 23 changed files with 5,551 additions and 73 deletions.
2 changes: 1 addition & 1 deletion BLAZAM/BLAZAM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<ServerGarbageCollection>false</ServerGarbageCollection>
<AssemblyVersion>0.8.8</AssemblyVersion>
<Version>2024.02.10.1718</Version>
<Version>2024.02.10.2119</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<RootNamespace>BLAZAM</RootNamespace>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
Expand Down
15 changes: 11 additions & 4 deletions BLAZAM/Middleware/UserStateMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ public UserStateMiddleware(RequestDelegate next)
_next = next;
}

public Task Invoke(HttpContext httpContext,ICurrentUserStateService currentUserStateService,IApplicationUserStateService userStateService)
public Task Invoke(HttpContext httpContext, ICurrentUserStateService currentUserStateService, IApplicationUserStateService userStateService)
{
currentUserStateService.State = userStateService.GetUserState(httpContext.User);
if (httpContext!=null && httpContext.Connection!=null && httpContext.Connection.RemoteIpAddress!=null && currentUserStateService.State!=null && currentUserStateService.State.IPAddress != httpContext.Connection.RemoteIpAddress)
currentUserStateService.State.IPAddress = httpContext.Connection.RemoteIpAddress;
if (httpContext != null && httpContext.User != null && httpContext.User.Identity != null)
{
if (httpContext.User.Identity.Name != null)
{

}
currentUserStateService.State = userStateService.GetUserState(httpContext.User);
if (httpContext.Connection != null && httpContext.Connection.RemoteIpAddress != null && currentUserStateService.State != null && currentUserStateService.State.IPAddress != httpContext.Connection.RemoteIpAddress)
currentUserStateService.State.IPAddress = httpContext.Connection.RemoteIpAddress;
}
return _next(httpContext);
}
}
Expand Down
57 changes: 17 additions & 40 deletions BLAZAM/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using BLAZAM.Server;
using BLAZAM.Services.Background;
using System.Net;
using BLAZAM.Database.Context;
using System.Diagnostics;

namespace BLAZAM
{
Expand Down Expand Up @@ -86,21 +88,23 @@ public static void Main(string[] args)

builder.InjectServices();

SetupKestrel(builder);


builder.Services.AddCors();


SetupKestrel(builder);


//Done with service injection let's build the App
AppInstance = builder.Build();

ApplicationInfo.services = AppInstance.Services;


// Configure the HTTP request pipeline.





Expand Down Expand Up @@ -135,7 +139,7 @@ public static void Main(string[] args)
// .SetIsOriginAllowed((host) => true)
// .AllowAnyMethod()
// .AllowAnyHeader());

AppInstance.UseCookiePolicy();
AppInstance.UseAuthentication();
AppInstance.UseAuthorization();
Expand All @@ -156,8 +160,12 @@ public static void Main(string[] args)

private static void SetupKestrel(WebApplicationBuilder builder)
{
//Temporary if during developementt
if (!ApplicationInfo.isUnderIIS)

var _programDbFactory = new AppDatabaseFactory(Configuration);
var kestrelContext = _programDbFactory.CreateDbContext();


if (!ApplicationInfo.isUnderIIS && !Debugger.IsAttached)
{
var listeningAddress = Configuration.GetValue<string>("ListeningAddress");
var httpPort = Configuration.GetValue<int>("HTTPPort");
Expand All @@ -167,7 +175,7 @@ private static void SetupKestrel(WebApplicationBuilder builder)
if (listeningAddress == "*")
{
options.ListenAnyIP(httpPort);
if (httpsPort != 0)
if (httpsPort != 0 && kestrelContext.AppSettings.FirstOrDefault()?.AppFQDN=="gsdfgfds")
{
options.ListenAnyIP(httpsPort, configure =>
{
Expand All @@ -179,9 +187,9 @@ private static void SetupKestrel(WebApplicationBuilder builder)
else
{
var ip = IPAddress.Parse(listeningAddress);

options.Listen(ip, httpPort);
if (httpsPort != 0)
if (httpsPort != 0 && kestrelContext.AppSettings.FirstOrDefault()?.AppFQDN == "gsdfgfds")
{
options.Listen(ip, httpsPort, configure =>
{
Expand Down Expand Up @@ -230,36 +238,5 @@ public static bool IsDevelopment
return AppInstance.Environment.IsDevelopment();
}
}


//public static void CheckWritablePathPermissions()
//{

// try
// {
// //Check permissions
// File.WriteAllText(WritablePath + @"writetest.test", "writetest");
// Writable = true;
// File.Delete(WritablePath + @"writetest.test");

// }
// catch (UnauthorizedAccessException)
// {
// Writable = false;
// Oops.ErrorMessage = "Applicatin Directory Error";
// Oops.DetailsMessage = "The application does not have write permission to the 'writable' directory.";
// }
// catch (DirectoryNotFoundException)
// {
// Writable = false;

// Oops.ErrorMessage = "Applicatin Directory Error";
// Oops.DetailsMessage = "The application's 'writable' directory is missing!";
// }

//}



}
}
2 changes: 0 additions & 2 deletions BLAZAM/ProgramHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ public static WebApplicationBuilder InjectServices(this WebApplicationBuilder bu
CultureInfo.DefaultThreadCurrentUICulture = culture;
*/

//Grab the connection string and store it in the context statically
//This can obviously only be changed on app restart


builder.Services.AddSingleton<ApplicationInfo>();
Expand Down
4 changes: 2 additions & 2 deletions BLAZAMCommon/Data/ApplicationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class ApplicationInfo
/// <summary>
/// Indicates whether Blazam is running under IIS or as a service
/// </summary>
public static bool isUnderIIS=>runningProcess.ProcessName.Contains("w3wp");
public static bool isUnderIIS=>runningProcess.ProcessName.Contains("w3wp")|| runningProcess.ProcessName.Contains("iisexpress");

/// <summary>
/// A local store of the .Net web application Services
Expand Down Expand Up @@ -148,7 +148,7 @@ public static bool installationCompleted
/// </summary>
public ApplicationInfo()
{

}

/// <summary>
Expand Down
23 changes: 10 additions & 13 deletions BLAZAMCommon/Data/WindowsImpersonation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ public static SafeAccessTokenHandle ImpersonatedToken
{
get
{
if (safeAccessTokenHandle == null)
{
// Call LogonUser to obtain a handle to an access token.


//Use interactive logon

bool returnValue = LogonUser(impersonationUser.Username, impersonationUser.FQDN!=null?impersonationUser.FQDN:"", impersonationUser.Password.ToPlainText(),
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeAccessTokenHandle);


Expand All @@ -42,16 +39,15 @@ public static SafeAccessTokenHandle ImpersonatedToken
throw new AuthenticationException(exception.Message);
}
}
}
return safeAccessTokenHandle;

}
set => safeAccessTokenHandle = value;
}

const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token.
const int LOGON32_LOGON_INTERACTIVE = 2;

const int LOGON32_LOGON_NETWORK = 9;


Expand All @@ -66,16 +62,17 @@ public WindowsImpersonation(WindowsImpersonationUser user)
{
impersonationUser = user;
}
public async Task<T> RunAsync<T>(Func<T> task) => await Task.Run(() => Run<T>(task));
public T Run<T>(Func<T> task)
public async Task<T?> RunAsync<T>(Func<T> task) => await Task.Run(() => Run<T>(task));
public T? Run<T>(Func<T> task)
{


T result = default;
T? result = default;

try
{
if (ImpersonatedToken==null || ImpersonatedToken.IsInvalid) throw new ApplicationException("The impersonation user is invalid. Check settings.");
var impersonatedToken = ImpersonatedToken;
if (impersonatedToken == null || impersonatedToken.IsInvalid) throw new ApplicationException("The impersonation user is invalid. Check settings.");

//Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No"));
// Check the identity.
Expand All @@ -84,7 +81,7 @@ public T Run<T>(Func<T> task)


WindowsIdentity.RunImpersonated(
ImpersonatedToken,
impersonatedToken,
() =>
{
// Check the identity.
Expand Down
4 changes: 2 additions & 2 deletions BLAZAMDatabase/Context/AppDatabaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class AppDatabaseFactory : IAppDatabaseFactory
/// </summary>
/// <param name="configuration"></param>
/// <param name="appInfo"></param>
public AppDatabaseFactory(IConfiguration configuration, ApplicationInfo appInfo)
public AppDatabaseFactory(IConfiguration configuration)
{
_configuration = configuration;

//Perform database auto update
ApplyDatabaseMigrations();
try
{
appInfo.InstallationCompleted = CheckInstallation();
ApplicationInfo.installationCompleted = CheckInstallation();
}
catch (DatabaseException ex)
{
Expand Down
Loading

0 comments on commit 4edb82d

Please sign in to comment.