Authentication.MagicLink is a library designed to provide magic link authentication support for ASP.NET Core applications. It streamlines the process of implementing passwordless authentication via one-time-use tokens sent to users through email. The library allows you to configure email providers, token generation, and validation for seamless integration with your application.
Before you can use Authentication.MagicLink, ensure your development environment meets the following requirements:
- .NET 7.0 or later
- ASP.NET Core 7.0 or later
To install the Authentication.MagicLink library, use the NuGet Package Manager. You can either use the command line or search for the package within Visual Studio. To install via the command line, run the following command:
dotnet add package Authentication.MagicLinkIn Visual Studio, you can search for the package by navigating to Tools > NuGet Package Manager > Manage NuGet Packages for Solution, then search for "Authentication.MagicLink" and install it to your desired project.
To use Authentication.MagicLink in your ASP.NET Core application, follow these steps:
-
Add the necessary
usingstatements at the top of yourProgram.csorStartup.csfile:using Authentication.MagicLink.Extensions; using Authentication.MagicLink.Services;
-
Configure the
Authentication.MagicLinkservices by calling theAddAuthenticationMagicLinkextension method inside yourConfigureServicesmethod:services .AddAuthenticationMagicLink(Configuration) .AddEmailProvider<MailKitEmailService>(); // Replace with your desired email provider
-
In your
Configuremethod, add the following lines to enable authentication:app.UseAuthentication(); app.UseAuthorization();
-
Use the
MapMagicLinkextension method to set up magic link authentication endpoints:app.MapMagicLink();
-
Secure your endpoints with the
[Authorize]attribute or theRequireAuthorizationmethod.
The AddAuthenticationMagicLink method accepts an IConfiguration instance that is used to configure magic link options. You can set these options in your appsettings.json file, with the following structure:
{
"MagicLink": {
"Issuer": "YourAppName",
"TokenLifeTimeMinutes": 15,
"TokenSecretKey": "YourTokenSecretKey",
"JwtIssuer": "YourJwtIssuer",
"JwtAudience": "YourJwtAudience",
"JwtSecretKey": "YourJwtSecretKey"
}
}Replace the placeholder values with your actual values.
You can customize various aspects of the magic link authentication process:
The library supports custom email providers. You'll need to create a class implementing the IEmailService interface and register it with the AddEmailProvider method.
You can customize token generation and validation by implementing the IMagicLinkService interface and registering your custom implementation with the AddMagicLinkService method.
The default endpoints for magic link authentication can be customized using the MapMagicLink method's optional parameters.
To contribute to the Authentication.MagicLink project, follow these steps:
- Clone the repository.
- Create a branch for your changes.
- Make changes and run tests.
- Submit a pull request with your changes.
To build and publish the NuGet package, use the following commands:
sh dotnet pack -c Release --include-symbols dotnet nuget push <PathToYour.nupkgFile> -k <YourNuGetApiKey> -s https://api.nuget.org/v3/index.json
Replace <PathToYour.nupkgFile> with the path to the generated .nupkg file and <YourNuGetApiKey> with your NuGet API key.
This code is provided for educational purposes and use in private projects and dev/test instances. Use in production environments is unsupported and at your own risk. Open derivative works are encouraged, but closed-source repurposing of this code is not.