SetNet.Auth.Jwt 1.1.0

dotnet add package SetNet.Auth.Jwt --version 1.1.0
                    
NuGet\Install-Package SetNet.Auth.Jwt -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SetNet.Auth.Jwt" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SetNet.Auth.Jwt" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SetNet.Auth.Jwt" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SetNet.Auth.Jwt --version 1.1.0
                    
#r "nuget: SetNet.Auth.Jwt, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SetNet.Auth.Jwt@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SetNet.Auth.Jwt&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SetNet.Auth.Jwt&version=1.1.0
                    
Install as a Cake Tool

<p align="center"> <img src="https://raw.githubusercontent.com/Povstalez/SetNet/master/assets/icon.png" alt="SetNet" width="96"> </p>

SetNet.Auth.Jwt

JWT-token authenticator for SetNet.Auth.

A drop-in IAuthenticator that validates a JWT bearer token — signature, issuer, audience, and lifetime — and maps a claim to the account id. Use it when your clients already hold a JWT (issued by your own login service or an identity provider) and you want the SetNet server to gate connections on it. For symmetric (shared-secret / HS256) tokens there's a one-line factory; for full control you pass your own TokenValidationParameters.

If your tokens are signed by an OpenID/OAuth provider whose signing keys rotate, use SetNet.Auth.OAuth instead — it fetches the provider's JWKS automatically.

Install

dotnet add package SetNet
dotnet add package SetNet.Auth
dotnet add package SetNet.Auth.Jwt

Setup

AuthRuntime.Enable();     // once at startup, both ends, before creating client/server

Server

Build the authenticator and hand it to UseAuth. UseAuth installs the enforced inbound gate from SetNet.Auth: until a peer authenticates, its application frames are dropped and only the auth handshake passes.

using SetNet.Auth;
using SetNet.Auth.Jwt;

// Symmetric (HS256) tokens signed with a shared secret:
var auth = JwtAuthenticator.WithSymmetricKey(
    secret:       "super-secret-signing-key",
    issuer:       "https://my-auth-service",   // optional; validated only when non-null
    audience:     "setnet-game",               // optional; validated only when non-null
    accountClaim: "sub");                       // claim carrying the account id

server.UseAuth(auth, new AuthOptions());

For asymmetric keys (RS256/ES256) or any advanced scenario, use the full constructor with your own TokenValidationParameters:

var validation = new TokenValidationParameters
{
    IssuerSigningKey         = new RsaSecurityKey(rsaPublicKey),
    ValidateIssuerSigningKey = true,
    ValidIssuer              = "https://my-auth-service",
    ValidateIssuer           = true,
    ValidAudience            = "setnet-game",
    ValidateAudience         = true,
    ValidateLifetime         = true,
};

var auth = new JwtAuthenticator(validation, accountClaim: "sub");
server.UseAuth(auth, new AuthOptions());

Client

Clients present their JWT as the auth token — nothing JWT-specific is needed beyond the base SetNet.Auth client hook:

// fixed token:
client.UseAuth(myJwt);

// or a provider that returns a fresh JWT on every (re)connect:
var auth = client.UseAuth(tokenProvider: () => accountService.GetFreshJwtAsync());

API

JwtAuthenticator : IAuthenticator

Member Purpose
JwtAuthenticator.WithSymmetricKey(secret, issuer?, audience?, accountClaim = "sub") factory for HMAC/HS256 tokens (issuer/audience validated only when non-null; lifetime on; 30 s clock skew)
new JwtAuthenticator(TokenValidationParameters validation, accountClaim = "sub") full control over validation (asymmetric keys, custom rules)
Task<AuthResult> AuthenticateAsync(string token) called by the server per handshake; returns AuthResult.Ok(id) / AuthResult.Fail(reason)

The account id is read from accountClaim, falling back to the standard sub claim; a missing/empty id fails the token. Any validation error (bad signature, expired, wrong issuer/audience) becomes an AuthResult.Fail carrying the exception message.

Notes

  • Use over TLS. A JWT is a bearer credential — anyone who sniffs it can replay it. Run SetNet with TLS (TLS-over-TCP, or wss:// via SetNet.WebSockets) so tokens aren't exposed on the wire.
  • Keep the secret server-side. For HS256 the same secret verifies and signs — never ship it in a client build. Prefer asymmetric (RS256/ES256) keys when the token is minted elsewhere.
  • Clock skew is 30 seconds in the WithSymmetricKey factory; set ClockSkew yourself on the full-control constructor if you need a different tolerance.
  • Session resume, multi-session policy, and the session store come from SetNet.Auth's AuthOptions — this package only decides whether a token is valid and who it belongs to.

Documentation & source

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 130 7/2/2026