SetNet.Auth
1.1.0
See the version list below for details.
dotnet add package SetNet.Auth --version 1.1.0
NuGet\Install-Package SetNet.Auth -Version 1.1.0
<PackageReference Include="SetNet.Auth" Version="1.1.0" />
<PackageVersion Include="SetNet.Auth" Version="1.1.0" />
<PackageReference Include="SetNet.Auth" />
paket add SetNet.Auth --version 1.1.0
#r "nuget: SetNet.Auth, 1.1.0"
#:package SetNet.Auth@1.1.0
#addin nuget:?package=SetNet.Auth&version=1.1.0
#tool nuget:?package=SetNet.Auth&version=1.1.0
<p align="center"> <img src="https://raw.githubusercontent.com/Povstalez/SetNet/master/assets/icon.png" alt="SetNet" width="96"> </p>
SetNet.Auth
Authentication & sessions for SetNet — by composition, no base class.
Plug it in and, until a peer authenticates, all of its application frames (regular messages and RPC) are dropped — only the auth handshake gets through. You validate the token; the package manages sessions, the enforced gate, and automatic reconnect-resume. Without this package, SetNet works fully open, as before.
🔒 Use over TLS (
UseSsl = true) so tokens aren't sent in the clear.
Install
dotnet add package SetNet
dotnet add package SetNet.MessagePack # or your own ISerializer
dotnet add package SetNet.Auth
At startup (once), before constructing your client/server:
using SetNet.Messaging;
using SetNet.MessagePack;
using SetNet.Auth;
SetNetSerializer.Use(new MessagePackNetSerializer());
AuthRuntime.Enable(); // ensures the auth handlers are discovered
Where does the token come from?
Not from SetNet. Your account/auth backend issues it out-of-band (HTTP login, OAuth, Steam/Apple/Google ticket, a guest token, …). The client presents that token; the server validates it via your IAuthenticator.
Server
Implement IAuthenticator (verify a JWT, call your backend, …) and enable auth on the server:
public class MyAuthenticator : IAuthenticator
{
public Task<AuthResult> AuthenticateAsync(string token)
{
// validate however you like:
if (TokenIsValid(token, out var accountId)) return Task.FromResult(AuthResult.Ok(accountId));
return Task.FromResult(AuthResult.Fail("invalid token"));
}
}
var server = new MyServer(config);
server.UseAuth(new MyAuthenticator(), new AuthOptions
{
MultiSession = MultiSessionPolicy.AllowMultiple, // or KickExisting / RejectNew
SessionTtl = TimeSpan.FromMinutes(2), // reconnect window (default in-memory store)
// SessionStore = new RedisSessionStore(...) // optional: survive restarts / share across a cluster
});
await server.StartAsync();
Inside your handlers, the peer is already authenticated (unauthenticated traffic never reaches them).
Client
Attach auth before connecting; it authenticates automatically on connect and every reconnect:
var client = new MyClient(config);
var auth = client.UseAuth(tokenProvider: () => accountService.GetFreshTokenAsync());
// (or a fixed token: client.UseAuth("my-token"))
await client.ConnectAsync();
var session = await auth.WhenAuthenticated; // throws AuthException if rejected
Console.WriteLine($"Logged in as {session.AccountId}");
// now send normally — the gate is open
await client.SayAsync("hi");
auth.IsAuthenticated,auth.Session, and theAuthenticated/AuthFailedevents are available too.
Reconnect & sessions
- After login the server issues a reconnect token; the client stores it and, on reconnect, resumes the same session automatically (within
SessionTtl). - The reconnect token rotates on every resume (single-use), so a captured token is short-lived; the client updates to the new one transparently.
- Idle sessions are evicted by a background sweep once past
SessionTtl, so dead sessions don't accumulate. - Pluggable store: sessions live in an in-process
MemorySessionStoreby default. ImplementISessionStore(async — Redis, a database, …) and setAuthOptions.SessionStoreto survive server restarts or share sessions across a cluster. - If the session has expired, the client falls back to a fresh login via your
tokenProvider(which can return a refreshed token) — also automatic. - Multi-session: the same account on two devices = two sessions by default (
AllowMultiple). UseKickExistingto disconnect the old device, orRejectNewto refuse the second login. Reconnect is always per-session, not per-account.
Notes
- Serializer-agnostic, depends only on
SetNet— the handshake is hand-framed (no MessagePack dependency). - Uses reserved wire type ids
65529/65530— don't use those for your own messages. - The gate never blocks heartbeat/system frames.
Documentation & source
License
MIT
| Product | Versions 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. |
-
.NETStandard 2.1
- SetNet (>= 1.1.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on SetNet.Auth:
| Package | Downloads |
|---|---|
|
SetNet.Auth.Jwt
JWT authenticator for SetNet.Auth: validates a JWT bearer token (signature, issuer, audience, lifetime) and maps a claim to the account id. server.UseAuth(new JwtAuthenticator(...)). Depends on SetNet.Auth + System.IdentityModel.Tokens.Jwt. |
|
|
SetNet.Auth.OAuth
OAuth 2.0 / OpenID Connect authenticator for SetNet.Auth: validates access (JWT) tokens against a provider's published JWKS (Auth0, Azure AD/Entra, Keycloak, Google, Cognito, …), auto-refreshing keys as they rotate. server.UseAuth(new OpenIdConnectAuthenticator(authority, audience)). Depends on SetNet.Auth + Microsoft.IdentityModel.Protocols.OpenIdConnect. |
|
|
SetNet.Redis
Redis backplane for SetNet: shared, restart-surviving implementations of ISessionStore (Auth), IBanStore (BanList) and IRoomStore (Rooms/Matchmaking) so sessions, bans and room codes work across a cluster of server nodes. Depends on SetNet.Auth + SetNet.Rooms + SetNet.BanList + StackExchange.Redis. |
GitHub repositories
This package is not used by any popular GitHub repositories.