diff --git a/Models/Token.cs b/Models/Token.cs new file mode 100644 index 0000000..e1310f9 --- /dev/null +++ b/Models/Token.cs @@ -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 +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 99cc324..d22fd0b 100644 --- a/Program.cs +++ b/Program.cs @@ -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(); diff --git a/TextSender-API.csproj b/TextSender-API.csproj index 3e29007..d4d3f0b 100644 --- a/TextSender-API.csproj +++ b/TextSender-API.csproj @@ -8,11 +8,13 @@ + +