Skip to content

Commit 8b07736

Browse files
committed
Added OwnsCredentials property to manage the scope of credentials.
1 parent 07cb886 commit 8b07736

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

NtApiDotNet/Win32/Rpc/Transport/RpcTransportSecurity.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,9 @@ internal IClientAuthenticationContext CreateClientContext()
203203
throw new ArgumentException($"Unsupported authentication level {AuthenticationLevel}");
204204
}
205205

206-
using (var creds = CredentialHandle.Create(GetAuthPackageName(),
207-
SecPkgCredFlags.Outbound, Credentials))
208-
{
209-
return new ClientAuthenticationContext(creds, GetContextRequestFlags(),
210-
ServicePrincipalName, SecDataRep.Native);
211-
}
206+
return new ClientAuthenticationContext(CredentialHandle.Create(GetAuthPackageName(),
207+
SecPkgCredFlags.Outbound, Credentials), GetContextRequestFlags(),
208+
ServicePrincipalName, SecDataRep.Native) { OwnsCredentials = true };
212209
}
213210
#endregion
214211
}

NtApiDotNet/Win32/Security/Authentication/ClientAuthenticationContext.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ private void Dispose(bool _)
6767
SecurityNativeMethods.DeleteSecurityContext(_context);
6868
_context = null;
6969
}
70+
if (OwnsCredentials)
71+
{
72+
_creds?.Dispose();
73+
}
7074
}
7175
#endregion
7276

@@ -185,6 +189,12 @@ private void Dispose(bool _)
185189
/// </summary>
186190
public bool IsLoopback => SecurityContextUtils.GetIsLoopback(Context);
187191

192+
/// <summary>
193+
/// Get or set whether the context owns the credentials object or not. If true
194+
/// then the credentials are disposed with the context.
195+
/// </summary>
196+
public bool OwnsCredentials { get; set; }
197+
188198
#endregion
189199

190200
#region Constructors

NtApiDotNet/Win32/Security/Authentication/ServerAuthenticationContext.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@ private SecStatusCode CallAccept(List<SecurityBuffer> input_buffers, List<Securi
5959
return result;
6060
}
6161

62-
6362
private void Dispose(bool _)
6463
{
6564
if (_context != null)
6665
{
6766
SecurityNativeMethods.DeleteSecurityContext(_context);
6867
}
68+
if (OwnsCredentials)
69+
{
70+
_creds?.Dispose();
71+
}
6972
}
7073

7174
private string GetTargetName()
@@ -196,6 +199,12 @@ private string GetTargetName()
196199
/// </summary>
197200
public bool IsLoopback => SecurityContextUtils.GetIsLoopback(Context);
198201

202+
/// <summary>
203+
/// Get or set whether the context owns the credentials object or not. If true
204+
/// then the credentials are disposed with the context.
205+
/// </summary>
206+
public bool OwnsCredentials { get; set; }
207+
199208
#endregion
200209

201210
#region Public Methods

0 commit comments

Comments
 (0)