pub struct ImpersonationToken { /* private fields */ }Expand description
An owned, immutable snapshot of a Windows impersonation context.
Cloning this value shares ownership of the same captured token. The native
handle is real rather than pseudo or borrowed, is not inheritable, and has
only TOKEN_IMPERSONATE access. No safe API exposes that handle or permits
callers to mutate the captured token. The value is safe to send to and share
between threads; each call to Self::with_impersonation affects only the
calling thread for the duration of its closure.
Implementations§
Source§impl ImpersonationToken
impl ImpersonationToken
Sourcepub fn capture() -> Result<ImpersonationToken, CaptureError>
pub fn capture() -> Result<ImpersonationToken, CaptureError>
Captures the calling thread’s effective Windows security context.
If the thread is impersonating, capture preserves its identification,
impersonation, or delegation level. If the thread has no token, capture
snapshots the process token as a SecurityImpersonation token. The
captured handle is non-inheritable and grants only TOKEN_IMPERSONATE.
§Errors
Returns a CaptureError synchronously when the effective token cannot
be opened, inspected, or duplicated. Anonymous impersonation is reported
as CaptureFailure::AnonymousContext.
Sourcepub fn with_impersonation<F, T>(&self, operation: F) -> Result<T, ApplyError>where
F: FnOnce() -> T,
pub fn with_impersonation<F, T>(&self, operation: F) -> Result<T, ApplyError>where
F: FnOnce() -> T,
Runs operation with this token applied to the current thread.
The exact prior thread-token state is restored before this method
returns, whether operation returns an ordinary value or a Result.
Stack unwinding also restores the prior state. The closure’s return value
is not interpreted, so a fallible closure produces
Result<Result<T, E>, ApplyError>.
This method changes only the calling thread’s impersonation state. The token may be reused sequentially or concurrently on other threads.
§Errors
Returns an ApplyError before calling operation when the exact prior
context cannot be saved or the captured token cannot be applied.
§Panics
Panics if restoring the exact prior thread-token state fails. Returning
a shared worker thread under an unknown identity would permit unrelated
later work to run with the wrong security context. If restoration fails
while operation is already unwinding, Rust’s double-panic behavior
aborts the process.
Trait Implementations§
Source§impl Clone for ImpersonationToken
impl Clone for ImpersonationToken
Source§fn clone(&self) -> ImpersonationToken
fn clone(&self) -> ImpersonationToken
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more