Titanis.Smb2. This page walks through setting up a project and connecting to a remote share to read a file.
Prerequisites
- .NET 8 SDK installed
- Access to a remote Windows host with SMB enabled
Getting started
Add package references
The SMB2 client depends on networking, I/O, and security packages. Add all of the following to your
.csproj:MySmb2App.csproj
Titanis.Security.Kerberos is listed here because SPNEGO can negotiate Kerberos in addition to NTLM. If you are certain your target environment only uses NTLM, you may omit it, but including it keeps your tool flexible.Configure credentials
Instantiate a
ClientCredentialDictionary and supply a factory that builds the authentication context for each connection. The factory receives the service principal name (spn) and the capabilities the server advertised, and returns a completed SpnegoClientContext:Program.cs
Configure name resolution and sockets
Smb2Client relies on service objects for name resolution and socket creation. For a single known target you can use DictionaryNameResolver to avoid a live DNS lookup:Program.cs
log with a logger instance from your application or null if you do not need connection-level logging.Create the client and open a file
Construct
Smb2Client with the credential and socket services, then open a remote file by its UNC path:Program.cs
Complete example
The following is the completeSmb2GetSample from the Titanis samples directory. It retrieves a file from a remote share using NTLM authentication over SPNEGO and writes it locally.
Program.cs
Authentication options
The credential factory returns aSpnegoClientContext, which can wrap multiple authentication mechanisms. NTLM (shown above) is the simplest option. To add Kerberos as a preferred mechanism, add a Kerberos context before the NTLM context in negoContext.Contexts: