Skip to content

Commit

Permalink
Faster AD Login
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Jacobsen Work committed Jun 13, 2023
1 parent 4f07696 commit 58ce274
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 168 deletions.
141 changes: 0 additions & 141 deletions BLAZAM/BLAZAM - Backup (1).csproj

This file was deleted.

4 changes: 1 addition & 3 deletions 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.2</AssemblyVersion>
<Version>2023.06.08.1745</Version>
<Version>2023.06.13.1919</Version>
<RootNamespace>BLAZAM</RootNamespace>
<GenerateDocumentationFile>False</GenerateDocumentationFile>

Expand All @@ -26,8 +26,6 @@
</ItemGroup>







Expand Down
50 changes: 26 additions & 24 deletions BLAZAMActiveDirectory/ActiveDirectoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public ActiveDirectoryContext(ActiveDirectoryContext activeDirectoryContextSeed)
AppRootDirectoryEntry = activeDirectoryContextSeed.AppRootDirectoryEntry;
_wmiFactory = activeDirectoryContextSeed._wmiFactory;
DomainControllers = activeDirectoryContextSeed.DomainControllers;
Status=activeDirectoryContextSeed.Status;
Status = activeDirectoryContextSeed.Status;
// UserStateService.UserStateAdded += PopulateUserStateDirectoryUser;
//ConnectAsync();
// _timer = new Timer(KeepAlive, null, 30000, 30000);
Expand All @@ -239,7 +239,7 @@ public ActiveDirectoryContext(ActiveDirectoryContext activeDirectoryContextSeed)

public List<DomainController> DomainControllers { get; private set; } = new();


private async void KeepAlive(object? state)
{
if (Status != DirectoryConnectionStatus.OK && Status != DirectoryConnectionStatus.Connecting)
Expand Down Expand Up @@ -288,7 +288,7 @@ public void Connect()

if (ad != null)
{
Loggers.ActiveDirectryLogger.Information("Active Directory settings found in database. {@DirectorySettings}",ad);
Loggers.ActiveDirectryLogger.Information("Active Directory settings found in database. {@DirectorySettings}", ad);

_authType = AuthenticationTypes.Secure;
if (ad.UseTLS)
Expand All @@ -306,7 +306,7 @@ public void Connect()

if (ad != null && ad.FQDN != null && ad.Username != null)
{
Loggers.ActiveDirectryLogger.Information("Checking Active Directory port status",ad.ServerAddress,ad.ServerPort);
Loggers.ActiveDirectryLogger.Information("Checking Active Directory port status", ad.ServerAddress, ad.ServerPort);

if (NetworkTools.IsPortOpen(ad.ServerAddress, ad.ServerPort))
{
Expand All @@ -316,7 +316,7 @@ public void Connect()
{
Loggers.ActiveDirectryLogger.Information("Connecting Active Directory context");
var pass = _encryption.DecryptObject<string>(ad.Password);
AppRootDirectoryEntry = new DirectoryEntry("LDAP://" + ad.ServerAddress + ":" + ad.ServerPort + "/" + ad.ApplicationBaseDN, ad.Username,pass , _authType);
AppRootDirectoryEntry = new DirectoryEntry("LDAP://" + ad.ServerAddress + ":" + ad.ServerPort + "/" + ad.ApplicationBaseDN, ad.Username, pass, _authType);
Loggers.ActiveDirectryLogger.Information("App Active Directory context connected");

RootDirectoryEntry = new DirectoryEntry("LDAP://" + ad.ServerAddress + ":" + ad.ServerPort + "/" + ad.FQDN.FqdnToDN(), ad.Username, pass, _authType);
Expand Down Expand Up @@ -400,7 +400,7 @@ public void Connect()
Status = DirectoryConnectionStatus.BadCredentials;
break;
default:
Loggers.ActiveDirectryLogger.Error("Error collecting domain controllers {@Error}",ex);
Loggers.ActiveDirectryLogger.Error("Error collecting domain controllers {@Error}", ex);
break;
}
}
Expand Down Expand Up @@ -443,7 +443,7 @@ public void Connect()


}
catch (Exception ex )
catch (Exception ex)
{
Loggers.ActiveDirectryLogger.Warning("Unexpected Error connecting to Active Directory {@Error}", ex);

Expand Down Expand Up @@ -472,7 +472,7 @@ public void Dispose()
{
try
{

var findUser = Users.FindUserByUsername(loginReq.Username.ToLower(), false);
if (findUser != null)
{
Expand All @@ -487,34 +487,36 @@ public void Dispose()
WindowsImpersonationUser logonUser = new WindowsImpersonationUser
{
FQDN = ConnectionSettings.FQDN,
Username=loginReq.Username,
Password= loginReq.SecurePassword
Username = loginReq.Username,
Password = loginReq.SecurePassword
};
WindowsImpersonation impersonation = new WindowsImpersonation(logonUser);
try
{
impersonation.Run(() => {
return true;
});
return findUser;
if (impersonation.Run(() =>
{
return true;
}))
return findUser;
}
catch(Exception ex)
catch (Exception ex)
{

return null;
}
using (var connection = new LdapConnection(new LdapDirectoryIdentifier(ConnectionSettings.ServerAddress, ConnectionSettings.ServerPort)))
{
connection.AuthType = AuthType.Basic;
connection.SessionOptions.ProtocolVersion = 3;
connection.SessionOptions.SecureSocketLayer = ConnectionSettings.UseTLS;

//using (var connection = new LdapConnection(new LdapDirectoryIdentifier(ConnectionSettings.ServerAddress, ConnectionSettings.ServerPort)))
//{
// connection.AuthType = AuthType.Basic;
// connection.SessionOptions.ProtocolVersion = 3;
// connection.SessionOptions.SecureSocketLayer = ConnectionSettings.UseTLS;
connection.Credential = new NetworkCredential(loginReq.Username, loginReq.SecurePassword);
connection.Bind();

// connection.Credential = new NetworkCredential(loginReq.Username, loginReq.SecurePassword);
// connection.Bind();
return findUser;

// return findUser;
}

//}

}
}
Expand Down
1 change: 1 addition & 0 deletions BLAZAMGui/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
@using BLAZAM.Gui.UI.Inputs
@using BLAZAM.Gui.Layouts
@using BLAZAM.Gui.UI.Outputs
@using BLAZAM.Gui.UI.Settings.Permissions

@using MudBlazor;

Expand Down

0 comments on commit 58ce274

Please sign in to comment.