Encamina.Enmarcha.AspNet.Mvc
8.1.6-preview-05
See the version list below for details.
dotnet add package Encamina.Enmarcha.AspNet.Mvc --version 8.1.6-preview-05
NuGet\Install-Package Encamina.Enmarcha.AspNet.Mvc -Version 8.1.6-preview-05
<PackageReference Include="Encamina.Enmarcha.AspNet.Mvc" Version="8.1.6-preview-05" />
paket add Encamina.Enmarcha.AspNet.Mvc --version 8.1.6-preview-05
#r "nuget: Encamina.Enmarcha.AspNet.Mvc, 8.1.6-preview-05"
// Install Encamina.Enmarcha.AspNet.Mvc as a Cake Addin #addin nuget:?package=Encamina.Enmarcha.AspNet.Mvc&version=8.1.6-preview-05&prerelease // Install Encamina.Enmarcha.AspNet.Mvc as a Cake Tool #tool nuget:?package=Encamina.Enmarcha.AspNet.Mvc&version=8.1.6-preview-05&prerelease
ASP.NET - MVC
ASP.NET MVC is a project designed to simplify the configuration and usage of MVC projects. It contains utilities related to authentication, authorization, binders, and more, with a focus on streamlining the use of common functionalities and reducing the necessary code for their implementation.
Setup
Nuget package
First, install NuGet. Then, install Encamina.Enmarcha.AspNet.Mvc from the package manager console:
PM> Install-Package Encamina.Enmarcha.AspNet.Mvc
.NET CLI:
First, install .NET CLI. Then, install Encamina.Enmarcha.AspNet.Mvc from the .NET CLI:
dotnet add package Encamina.Enmarcha.AspNet.Mvc
How to use
Basic Authorization
First, you need to add the BasicApiKeyOptions to your project configuration. You can achieve this by using any configuration provider. The followng code is an example of how the settings would appear using the appsettings.json file:
{
// ...
"BasicApiKeyOptions": {
"ApiKeys": {
// Dictionary that relates an API key client unique identifier its expected API key.
"WeatherAuthoringKey": "123456",
"AnimalsAuthoringKey": "00000",
}
},
// ...
}
Next, in Program.cs
or a similar entry point file in your MVC project, add the following code.
// Entry point
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
// ...
});
// ...
// Or others configuration providers...
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
// ...
builder.Services
.AddControllers()
.AddApiKeyAuthorizationFilter();
builder.Services
.AddBasicApiKeyAuthorization(builder.Configuration);
// ...
In the JSON, different API keys and their values are defined. Now, it is only necessary to decorate any controller with the ApiKey
attribute, and automatically, each time a request is made, it must contain the x-api-key
header with the corresponding value of the key referenced by the attribute.
[ApiController]
[ApiKey("WeatherAuthoringKey")]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
public WeatherForecastController()
{
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
As observed, the WeatherForecastController
controller is decorated with the ApiKey
attribute, which refers to WeatherAuthoringKey
. Therefore, any request that does not contain the 'x-api-key' header with the value 123456
will result in an Unauthorized 401 error.
curl --location 'https://yourendpoint.net/weatherforecast' \
--header 'x-api-key: 123456'
# Response: 200 OK
curl --location 'https://yourendpoint.net/weatherforecast' \
--header 'x-api-key: 465465456'
# Response: 401 Unauthorized
curl --location 'https://yourendpoint.net/weatherforecast' \
# Response: 401 Unauthorized
Authentication
There are several extension methods that facilitate the configuration of Authentication, both OpenID Connect and JWT-bearer.
JWT-bearer with Azure Active Directory authentication
First, you need to add the AzureActiveDirectoryOptions to your project configuration. You can achieve this by using any configuration provider. The followng code is an example of how the settings would appear using the appsettings.json file:
{
// ...
"AzureActiveDirectoryOptions": {
"ClientId" : "", // Azure Active Directory client's ID (sometimes also called Application ID).
"ClientSecret" : "", // Client's secret on Azure Active Directory.
"Instance" : "", // Instance
"Domain" : "", // Domain
"TenantId" : "", // Azure's tenant ID
"CallbackPath" : "" // Callback path, which sometimes is just an URL
},
// ...
}
Next, in a Program.cs
or a similar entry point file in your MVC project, add the following code.
// Entry point
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
// ...
});
// ...
// Or others configuration providers...
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
// ...
builder.Services
.AddAuthentication(options =>
{
// ...
})
.AddJwtBearerAuthentication();
// ...
OpenId Connect with Azure Active Directory authentication
First, you need to add the AzureActiveDirectoryOptions to your project configuration. You can achieve this by using any configuration provider. The followng code is an example of how the settings would appear using the appsettings.json file:
{
// ...
"AzureActiveDirectoryOptions": {
"ClientId" : "", // Azure Active Directory client's ID (sometimes also called Application ID).
"ClientSecret" : "", // Client's secret on Azure Active Directory.
"Instance" : "", // Instance
"Domain" : "", // Domain
"TenantId" : "", // Azure's tenant ID
"CallbackPath" : "" // Callback path, which sometimes is just an URL
},
// ...
}
Next, in a Program.cs
or a similar entry point file in your MVC project, add the following code.
// Entry point
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
// ...
});
// ...
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
// ...
builder.Services
.AddAuthentication(options =>
{
// ...
})
.AddOpenIdConnectAuthentication();
// ...
Binding
CustomDateTimeModelBinderProvider
CustomDateTimeModelBinderProvider provides custom DateTime
model binders as a valid IModelBinderProvider
that can be registered in MvcOptions.
// Entry point
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
// ...
});
// ...
builder.Services
.AddMvcCore(options =>
{
// ...
options.ModelBinderProviders.Insert(0, new CustomDateTimeModelBinderProvider());
// ...
});
// ...
Now, if we have, for example, an endpoint that receives a DateTime
, it can be parsed from different string
formats.
// ... Controller method
[HttpGet]
public string GetDatetime(DateTime? time)
{
return time.ToString();
}
curl --location 'https://yourendpoint/test?time=20051203'
# Response: 200 OK, 12/3/2005 12:00:00 AM
FormToDictionaryModelBinder
FormToDictionaryModelBinder binds the incoming form in the request to a dictionary. It checks if the request has a form content type, and if so, it retrieves the form data. If the form data contains the specified field name, it attempts to deserialize the corresponding value into a dictionary of type <TKey, TValue>
using JSON deserialization.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- CommunityToolkit.Diagnostics (>= 8.2.2)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.4)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 8.0.4)
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 |
---|---|---|
8.2.0 | 70 | 10/22/2024 |
8.2.0-preview-01-m01 | 102 | 9/17/2024 |
8.1.9-preview-02 | 47 | 10/22/2024 |
8.1.9-preview-01 | 175 | 10/4/2024 |
8.1.8 | 147 | 9/23/2024 |
8.1.8-preview-07 | 146 | 9/12/2024 |
8.1.8-preview-06 | 146 | 9/11/2024 |
8.1.8-preview-05 | 82 | 9/10/2024 |
8.1.8-preview-04 | 211 | 8/16/2024 |
8.1.8-preview-03 | 124 | 8/13/2024 |
8.1.8-preview-02 | 96 | 8/13/2024 |
8.1.8-preview-01 | 95 | 8/12/2024 |
8.1.7 | 96 | 8/7/2024 |
8.1.7-preview-09 | 100 | 7/3/2024 |
8.1.7-preview-08 | 62 | 7/2/2024 |
8.1.7-preview-07 | 78 | 6/10/2024 |
8.1.7-preview-06 | 93 | 6/10/2024 |
8.1.7-preview-05 | 73 | 6/6/2024 |
8.1.7-preview-04 | 88 | 6/6/2024 |
8.1.7-preview-03 | 90 | 5/24/2024 |
8.1.7-preview-02 | 84 | 5/10/2024 |
8.1.7-preview-01 | 68 | 5/8/2024 |
8.1.6 | 1,007 | 5/7/2024 |
8.1.6-preview-08 | 63 | 5/2/2024 |
8.1.6-preview-07 | 79 | 4/29/2024 |
8.1.6-preview-06 | 254 | 4/26/2024 |
8.1.6-preview-05 | 93 | 4/24/2024 |
8.1.6-preview-04 | 96 | 4/22/2024 |
8.1.6-preview-03 | 87 | 4/22/2024 |
8.1.6-preview-02 | 114 | 4/17/2024 |
8.1.6-preview-01 | 177 | 4/15/2024 |
8.1.5 | 123 | 4/15/2024 |
8.1.5-preview-15 | 99 | 4/10/2024 |
8.1.5-preview-14 | 85 | 3/20/2024 |
8.1.5-preview-13 | 88 | 3/18/2024 |
8.1.5-preview-12 | 92 | 3/13/2024 |
8.1.5-preview-11 | 91 | 3/13/2024 |
8.1.5-preview-10 | 93 | 3/13/2024 |
8.1.5-preview-09 | 94 | 3/12/2024 |
8.1.5-preview-08 | 87 | 3/12/2024 |
8.1.5-preview-07 | 90 | 3/8/2024 |
8.1.5-preview-06 | 180 | 3/8/2024 |
8.1.5-preview-05 | 79 | 3/7/2024 |
8.1.5-preview-04 | 63 | 3/7/2024 |
8.1.5-preview-03 | 82 | 3/7/2024 |
8.1.5-preview-02 | 131 | 2/28/2024 |
8.1.5-preview-01 | 119 | 2/19/2024 |
8.1.4 | 183 | 2/15/2024 |
8.1.3 | 123 | 2/13/2024 |
8.1.3-preview-07 | 76 | 2/13/2024 |
8.1.3-preview-06 | 85 | 2/12/2024 |
8.1.3-preview-05 | 94 | 2/9/2024 |
8.1.3-preview-04 | 87 | 2/8/2024 |
8.1.3-preview-03 | 93 | 2/7/2024 |
8.1.3-preview-02 | 84 | 2/2/2024 |
8.1.3-preview-01 | 83 | 2/2/2024 |
8.1.2 | 126 | 2/1/2024 |
8.1.2-preview-9 | 103 | 1/22/2024 |
8.1.2-preview-8 | 84 | 1/19/2024 |
8.1.2-preview-7 | 88 | 1/19/2024 |
8.1.2-preview-6 | 80 | 1/19/2024 |
8.1.2-preview-5 | 86 | 1/19/2024 |
8.1.2-preview-4 | 87 | 1/19/2024 |
8.1.2-preview-3 | 78 | 1/18/2024 |
8.1.2-preview-2 | 86 | 1/18/2024 |
8.1.2-preview-16 | 70 | 1/31/2024 |
8.1.2-preview-15 | 69 | 1/31/2024 |
8.1.2-preview-14 | 81 | 1/25/2024 |
8.1.2-preview-13 | 88 | 1/25/2024 |
8.1.2-preview-12 | 87 | 1/23/2024 |
8.1.2-preview-11 | 89 | 1/23/2024 |
8.1.2-preview-10 | 86 | 1/22/2024 |
8.1.2-preview-1 | 87 | 1/18/2024 |
8.1.1 | 110 | 1/18/2024 |
8.1.0 | 106 | 1/18/2024 |
8.0.3 | 167 | 12/29/2023 |
8.0.1 | 129 | 12/14/2023 |
8.0.0 | 134 | 12/7/2023 |
6.0.4.3 | 115 | 12/29/2023 |
6.0.4.2 | 112 | 12/20/2023 |
6.0.4.1 | 171 | 12/19/2023 |
6.0.4 | 129 | 12/4/2023 |
6.0.3.20 | 130 | 11/27/2023 |
6.0.3.19 | 156 | 11/22/2023 |