Browse Source

add: authentication dependencies

authentication
KerelOlivier 2 years ago
parent
commit
0460559df4
  1. 8
      DTO/UserDTO.cs
  2. 14
      Models/UserIdentity.cs
  3. 9
      Program.cs
  4. 2
      backend.csproj

8
DTO/UserDTO.cs

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
namespace backend.DTO;
public class UserDTO
{
public string UserName { get; set; }
public string? Email { get; set; }
public string Password { get; set; }
}

14
Models/UserIdentity.cs

@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
using MongoDB.Bson.Serialization.Attributes;
namespace backend.Models;
public class UserIdentity
{
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string Id { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Role { get; set; }
}

9
Program.cs

@ -1,5 +1,8 @@ @@ -1,5 +1,8 @@
using System.Text;
using backend.Models;
using backend.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
var builder = WebApplication.CreateBuilder(args);
@ -7,7 +10,10 @@ var builder = WebApplication.CreateBuilder(args); @@ -7,7 +10,10 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<BlogDatabaseSettings>(builder.Configuration.GetSection("BlogDatabase"));
//Authentication
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer();
builder.Services.AddSingleton<UserService>();
builder.Services.AddControllers();
@ -22,11 +28,12 @@ if (app.Environment.IsDevelopment()) @@ -22,11 +28,12 @@ if (app.Environment.IsDevelopment())
{
app.UseStaticFiles();
app.UseSwagger();
app.UseSwaggerUI(c=> c.InjectStylesheet("/swagger-ui/darkMode.css"));
app.UseSwaggerUI(c => c.InjectStylesheet("/swagger-ui/darkMode.css"));
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

2
backend.csproj

@ -7,8 +7,10 @@ @@ -7,8 +7,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.5" />
<PackageReference Include="MongoDB.Driver" Version="2.15.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.18.0" />
</ItemGroup>
</Project>

Loading…
Cancel
Save