ClosureOSS.WebPush 2.0.1

dotnet add package ClosureOSS.WebPush --version 2.0.1
                    
NuGet\Install-Package ClosureOSS.WebPush -Version 2.0.1
                    
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="ClosureOSS.WebPush" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ClosureOSS.WebPush" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ClosureOSS.WebPush" />
                    
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 ClosureOSS.WebPush --version 2.0.1
                    
#r "nuget: ClosureOSS.WebPush, 2.0.1"
                    
#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 ClosureOSS.WebPush@2.0.1
                    
#: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=ClosureOSS.WebPush&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ClosureOSS.WebPush&version=2.0.1
                    
Install as a Cake Tool

WebPush Csharp library

Build

Why

To deliver generic events using HTTP Push as outlined in Generic Event Delivery Using HTTP, backend-triggered push messages must be encrypted. This is accomplished using the Message Encryption for Web Push standard, which relies on Voluntary Application Server Identification (VAPID) for Web Push (RFC8292) for authentication. Furthermore, any data included with the push message must be separately encrypted following the rules of Encrypted Content-Encoding for HTTP (RFC8188).

This package makes it easy to send push notifications from an application server.

Purpose of fork

Support for the "aes128gcm" HTTP Content Coding.

Rewrite using System.Security.Crytography, Microsoft.IdentityModel and other first party interfaces.

Install

Installation is simple, just install via

dotnet add package ClosureOSS.WebPush

Usage

The common use case for this library is an application server using VAPID keys.

using WebPush;

var pushEndpoint = @"https://fcm.googleapis.com/fcm/send/efz_TLX_rLU:APA91bE6U0iybLYvv0F3mf6uDLB6....";
var p256dh = @"BKK18ZjtENC4jdhAAg9OfJacySQiDVcXMamy3SKKy7FwJcI5E0DKO9v4V2Pb8NnAPN4EVdmhO............";
var auth = @"fkJatBBEl...............";

var subject = @"mailto:example@example.com";
var publicKey = @"BDjASz8kkVBQJgWcD05uX3VxIs_gSHyuS023jnBoHBgUbg8zIJvTSQytR8MP4Z3-kzcGNVnM...............";
var privateKey = @"mryM-krWj_6IsIMGsd8wNFXGBxnx...............";

var subscription = new PushSubscription(pushEndpoint, p256dh, auth);
var vapidDetails = new VapidDetails(subject, publicKey, privateKey);

var webPushClient = new WebPushClient();
try
{
	await webPushClient.SendNotificationAsync(subscription, "payload", vapidDetails);
}
catch (WebPushException exception)
{
	Console.WriteLine("Http STATUS code" + exception.StatusCode);
}

API Reference

SendNotificationAsync(pushSubscription, payload, vapidDetails|options, cancellationToken)

var subscription = new PushSubscription(pushEndpoint, p256dh, auth);

var options = new WebPushOptions
{
  VapidDetails = new VapidDetails(subject, publicKey, privateKey),
  Topic = "RTQ.....",
};

var webPushClient = new WebPushClient();
try
{
	webPushClient.SendNotificationAsync(subscription, "payload", options);
}
catch (WebPushException exception)
{
	Console.WriteLine("Http STATUS code" + exception.StatusCode);
}

Note: SendNotificationAsync() you don't need to define a payload, and this method will work without a VAPID keys if the push service supports it.

Input

Push Subscription

The first argument must be an PushSubscription object containing the details for a push subscription.

Payload

The payload is optional, but if set, will be the data sent with a push message.

This must be a string

Note: In order to encrypt the payload, the pushSubscription must have a keys object with p256dh and auth values.

Options

Options is an optional argument that if defined should be an Dictionary<string,object> containing any of the following values defined, although none of them are required.

<hr />

GenerateVapidKeys()

var vapidKeys = VapidHelper.GenerateVapidKeys();

// Prints 2 URL Safe Base64 Encoded Strings
Console.WriteLine("Public {0}", vapidKeys.PublicKey);
Console.WriteLine("Private {0}", vapidKeys.PrivateKey);

Input

None.

Returns

Returns a VapidDetails object with PublicKey and PrivateKey values populated which are URL Safe Base64 encoded strings.

Note: You should create these keys once, store them and use them for all future messages you send.

<hr />

GetVapidHeaders(audience, subject, publicKey, privateKey, expiration)

Uri uri = new Uri(subscription.Endpoint);
string audience = uri.Scheme + Uri.SchemeDelimiter + uri.Host;

Dictionary<string, string> vapidHeaders = VapidHelper.GetVapidHeaders(
  audience,
  @"mailto: example@example.com",
  publicKey,
  privateKey
);

The GetVapidHeaders() method will take in the values needed to create an Authorization and Crypto-Key header.

Input

The GetVapidHeaders() method expects the following input:

  • audience: the origin of the push service.
  • subject: the mailto or URL for your application.
  • publicKey: the VAPID public key.
  • privateKey: the VAPID private key.

Returns

This method returns a Dictionary<string, string> intented to be headers of a web request. It will contain the following key(s):

  • Authorization

<hr />

Credits

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
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
2.0.1 121 9/2/2025