Encamina.Enmarcha.AspNet.Mvc
8.1.7
See the version list below for details.
dotnet add package Encamina.Enmarcha.AspNet.Mvc --version 8.1.7
NuGet\Install-Package Encamina.Enmarcha.AspNet.Mvc -Version 8.1.7
<PackageReference Include="Encamina.Enmarcha.AspNet.Mvc" Version="8.1.7" />
paket add Encamina.Enmarcha.AspNet.Mvc --version 8.1.7
#r "nuget: Encamina.Enmarcha.AspNet.Mvc, 8.1.7"
// Install Encamina.Enmarcha.AspNet.Mvc as a Cake Addin #addin nuget:?package=Encamina.Enmarcha.AspNet.Mvc&version=8.1.7 // Install Encamina.Enmarcha.AspNet.Mvc as a Cake Tool #tool nuget:?package=Encamina.Enmarcha.AspNet.Mvc&version=8.1.7
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.6)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 8.0.6)
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 | 120 | 10/22/2024 |
8.2.0-preview-01-m01 | 105 | 9/17/2024 |
8.1.9-preview-03 | 57 | 11/19/2024 |
8.1.9-preview-02 | 50 | 10/22/2024 |
8.1.9-preview-01 | 178 | 10/4/2024 |
8.1.8 | 152 | 9/23/2024 |
8.1.8-preview-07 | 264 | 9/12/2024 |
8.1.8-preview-06 | 149 | 9/11/2024 |
8.1.8-preview-05 | 85 | 9/10/2024 |
8.1.8-preview-04 | 214 | 8/16/2024 |
8.1.8-preview-03 | 127 | 8/13/2024 |
8.1.8-preview-02 | 99 | 8/13/2024 |
8.1.8-preview-01 | 98 | 8/12/2024 |
8.1.7 | 101 | 8/7/2024 |
8.1.7-preview-09 | 103 | 7/3/2024 |
8.1.7-preview-08 | 65 | 7/2/2024 |
8.1.7-preview-07 | 81 | 6/10/2024 |
8.1.7-preview-06 | 96 | 6/10/2024 |
8.1.7-preview-05 | 78 | 6/6/2024 |
8.1.7-preview-04 | 91 | 6/6/2024 |
8.1.7-preview-03 | 93 | 5/24/2024 |
8.1.7-preview-02 | 88 | 5/10/2024 |
8.1.7-preview-01 | 72 | 5/8/2024 |
8.1.6 | 1,143 | 5/7/2024 |
8.1.6-preview-08 | 67 | 5/2/2024 |
8.1.6-preview-07 | 83 | 4/29/2024 |
8.1.6-preview-06 | 262 | 4/26/2024 |
8.1.6-preview-05 | 97 | 4/24/2024 |
8.1.6-preview-04 | 100 | 4/22/2024 |
8.1.6-preview-03 | 91 | 4/22/2024 |
8.1.6-preview-02 | 120 | 4/17/2024 |
8.1.6-preview-01 | 183 | 4/15/2024 |
8.1.5 | 129 | 4/15/2024 |
8.1.5-preview-15 | 103 | 4/10/2024 |
8.1.5-preview-14 | 89 | 3/20/2024 |
8.1.5-preview-13 | 91 | 3/18/2024 |
8.1.5-preview-12 | 95 | 3/13/2024 |
8.1.5-preview-11 | 94 | 3/13/2024 |
8.1.5-preview-10 | 96 | 3/13/2024 |
8.1.5-preview-09 | 97 | 3/12/2024 |
8.1.5-preview-08 | 90 | 3/12/2024 |
8.1.5-preview-07 | 95 | 3/8/2024 |
8.1.5-preview-06 | 185 | 3/8/2024 |
8.1.5-preview-05 | 82 | 3/7/2024 |
8.1.5-preview-04 | 66 | 3/7/2024 |
8.1.5-preview-03 | 85 | 3/7/2024 |
8.1.5-preview-02 | 136 | 2/28/2024 |
8.1.5-preview-01 | 122 | 2/19/2024 |
8.1.4 | 188 | 2/15/2024 |
8.1.3 | 128 | 2/13/2024 |
8.1.3-preview-07 | 79 | 2/13/2024 |
8.1.3-preview-06 | 88 | 2/12/2024 |
8.1.3-preview-05 | 97 | 2/9/2024 |
8.1.3-preview-04 | 90 | 2/8/2024 |
8.1.3-preview-03 | 96 | 2/7/2024 |
8.1.3-preview-02 | 89 | 2/2/2024 |
8.1.3-preview-01 | 86 | 2/2/2024 |
8.1.2 | 131 | 2/1/2024 |
8.1.2-preview-9 | 106 | 1/22/2024 |
8.1.2-preview-8 | 89 | 1/19/2024 |
8.1.2-preview-7 | 91 | 1/19/2024 |
8.1.2-preview-6 | 83 | 1/19/2024 |
8.1.2-preview-5 | 89 | 1/19/2024 |
8.1.2-preview-4 | 90 | 1/19/2024 |
8.1.2-preview-3 | 83 | 1/18/2024 |
8.1.2-preview-2 | 89 | 1/18/2024 |
8.1.2-preview-16 | 73 | 1/31/2024 |
8.1.2-preview-15 | 72 | 1/31/2024 |
8.1.2-preview-14 | 86 | 1/25/2024 |
8.1.2-preview-13 | 91 | 1/25/2024 |
8.1.2-preview-12 | 90 | 1/23/2024 |
8.1.2-preview-11 | 92 | 1/23/2024 |
8.1.2-preview-10 | 91 | 1/22/2024 |
8.1.2-preview-1 | 90 | 1/18/2024 |
8.1.1 | 115 | 1/18/2024 |
8.1.0 | 111 | 1/18/2024 |
8.0.3 | 174 | 12/29/2023 |
8.0.1 | 134 | 12/14/2023 |
8.0.0 | 139 | 12/7/2023 |
6.0.4.3 | 122 | 12/29/2023 |
6.0.4.2 | 117 | 12/20/2023 |
6.0.4.1 | 176 | 12/19/2023 |
6.0.4 | 136 | 12/4/2023 |
6.0.3.20 | 135 | 11/27/2023 |
6.0.3.19 | 161 | 11/22/2023 |