Adding token support

This commit is contained in:
kdeng00
2023-09-29 19:33:37 -04:00
parent 36e3722a01
commit 616bd97b64
3 changed files with 40 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
using Newtonsoft.Json;
namespace TextSender_API.Models;
public class Token
{
#region Properties
[JsonProperty("access_token")]
public string? AccessToken { get; set; }
[JsonProperty("issued")]
public DateTime? Issued { get; set; }
#endregion
}
+25
View File
@@ -1,3 +1,11 @@
using System.Text;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
@@ -11,6 +19,22 @@ builder.Services.AddSwaggerGen();
var app = builder.Build();
var connString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = builder.Configuration["JWT:Audience"],
ValidIssuer = builder.Configuration["JWT:Issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:Secret"]!))
};
});
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
@@ -21,6 +45,7 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
+2
View File
@@ -8,11 +8,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.11" />
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.2" />
</ItemGroup>
</Project>