Step 1: Create .net core order api
Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
</ItemGroup>
</Project>
Step 2: Microsoft.AspNetCore.Authentication.JwtBearer and Microsoft.AspNetCore.Mvc.Versioning nuget package
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
</ItemGroup>
</Project>
Step 3: Add AddAuthetication with certificate access and AddApiVersioning in ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddApiVersioning(v=>{
v.DefaultApiVersion = new ApiVersion(3, 0);
v.ApiVersionReader = new HeaderApiVersionReader("api-version");
v.AssumeDefaultVersionWhenUnspecified = true;
v.Conventions.Controller<OrderController>()
.HasApiVersion(1, 0)
.HasApiVersion(2, 0)
.HasApiVersion(3, 0)
.Action(f => f.Get()).MapToApiVersion(3, 0)
.Action(f=>f.GetV1()).MapToApiVersion(1,0)
.Action(f=>f.GetV2()).MapToApiVersion(2,0);
});
X509Certificate2 x509Certificate2 = new X509Certificate2(@"C:\Projects\Keys\public.cer");
X509SecurityKey x509SecurityKey = new X509SecurityKey(x509Certificate2);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateIssuerSigningKey=true,
ValidAudience = "www.errorandsolution.com",
ValidIssuer = "www.errorandsolution.com",
IssuerSigningKey = x509SecurityKey
};
});
}
Step 4: add UseAuthentication and UseAuthorization in Configure
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
Step 5: Create Order controller and add Authorize and version route
namespace FST.EasyCart.Order.Controllers
{
[Route("api/v{version:apiversion}/[controller]")]
//[ApiVersion("1.0")]
//[ApiVersion("2.0")]
//[ApiVersion("3.0")]
[ApiController]
[Authorize]
public class OrderController : ControllerBase
{
// GET: api/<OrderController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
[HttpGet]
// [MapToApiVersion("1.0")]
public IEnumerable<string> GetV1()
{
return new string[] { "version 1", "version 1 data" };
}
[HttpGet]
// [MapToApiVersion("2.0")]
public IEnumerable<string> GetV2()
{
return new string[] { "version 2", "version 2 data" };
}
// GET api/<OrderController>/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
// POST api/<OrderController>
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/<OrderController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<OrderController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}