mirror of
https://github.com/ryzij/AMessanger-Server.git
synced 2026-09-18 19:25:38 +00:00
Compare commits
3 Commits
main
...
c70b9b6174
| Author | SHA1 | Date | |
|---|---|---|---|
| c70b9b6174 | |||
| 90143e180f | |||
| c8c5b3b245 |
@@ -1,3 +1,4 @@
|
|||||||
|
using AMessanger.Application;
|
||||||
using AMessanger.Application.Hubs;
|
using AMessanger.Application.Hubs;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -12,6 +13,8 @@ builder.Services.AddSwaggerGen();
|
|||||||
|
|
||||||
builder.Services.AddSignalR();
|
builder.Services.AddSignalR();
|
||||||
|
|
||||||
|
builder.Services.AddAuth(builder.Configuration);
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SignalR" />
|
<PackageReference Include="Microsoft.AspNetCore.SignalR" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using System.Text;
|
||||||
|
using AMessanger.Application.Settings;
|
||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
namespace AMessanger.Application;
|
||||||
|
|
||||||
|
public static class ApplicationExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddAuth(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var jwtConf = configuration.GetSection(JwtSettings.SECTION_NAME);
|
||||||
|
services.Configure<JwtSettings>(jwtConf);
|
||||||
|
var settings = jwtConf.Get<JwtSettings>()!;
|
||||||
|
|
||||||
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||||
|
.AddJwtBearer(options =>
|
||||||
|
{
|
||||||
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
|
{
|
||||||
|
ValidateIssuer = true,
|
||||||
|
ValidateAudience = true,
|
||||||
|
ValidateLifetime = true,
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
|
||||||
|
ValidIssuer = settings.Issuer,
|
||||||
|
ValidAudience = settings.Audience,
|
||||||
|
IssuerSigningKey = new SymmetricSecurityKey(
|
||||||
|
Encoding.UTF8.GetBytes(settings.Key)
|
||||||
|
)
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return services.AddAuthorization();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
|
using AMessanger.Application.Settings;
|
||||||
|
using AMessanger.Core.Abstraction;
|
||||||
|
using AMessanger.Core.Models;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
|
namespace AMessanger.Application.Services;
|
||||||
|
|
||||||
|
public class JwtService(IOptions<JwtSettings> options) : IJwtService
|
||||||
|
{
|
||||||
|
private readonly JwtSettings _settings = options.Value;
|
||||||
|
|
||||||
|
public string GenerateToken(User user)
|
||||||
|
{
|
||||||
|
var claims = new[]
|
||||||
|
{
|
||||||
|
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
|
||||||
|
new Claim(ClaimTypes.Name, user.Name)
|
||||||
|
};
|
||||||
|
|
||||||
|
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.Key));
|
||||||
|
|
||||||
|
var creditnails = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||||
|
|
||||||
|
var token = new JwtSecurityToken(
|
||||||
|
issuer: _settings.Issuer,
|
||||||
|
audience: _settings.Audience,
|
||||||
|
claims: claims,
|
||||||
|
expires: DateTime.UtcNow.AddMinutes(_settings.ExpiresMinutes),
|
||||||
|
signingCredentials: creditnails
|
||||||
|
);
|
||||||
|
|
||||||
|
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace AMessanger.Application.Settings;
|
||||||
|
|
||||||
|
public class JwtSettings
|
||||||
|
{
|
||||||
|
public const string SECTION_NAME = "Jwt";
|
||||||
|
|
||||||
|
public string Issuer { get; set; } = null!;
|
||||||
|
public string Audience { get; set; } = null!;
|
||||||
|
public int ExpiresMinutes { get; set; }
|
||||||
|
public string Key { get; set; } = null!;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using AMessanger.Core.Models;
|
||||||
|
|
||||||
|
namespace AMessanger.Core.Abstraction;
|
||||||
|
|
||||||
|
public interface IJwtService
|
||||||
|
{
|
||||||
|
public string GenerateToken(User user);
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace AMessanger.Core.Models;
|
||||||
|
|
||||||
|
public class User(Guid id, string name, string hashedPassword)
|
||||||
|
{
|
||||||
|
public Guid Id { get; private set; } = id;
|
||||||
|
public string Name { get; set; } = name;
|
||||||
|
public string HashedPassword { get; set; } = hashedPassword;
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.11" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.11" />
|
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.11" />
|
||||||
<PackageVersion Include="Microsoft.AspNetCore.SignalR" Version="1.2.12" />
|
<PackageVersion Include="Microsoft.AspNetCore.SignalR" Version="1.2.12" />
|
||||||
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
|
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
|
||||||
|
|||||||
Reference in New Issue
Block a user