Adding authentication and authorization flow
This commit is contained in:
38
MainProject/Services/AuthService.cs
Normal file
38
MainProject/Services/AuthService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||
using BasicDotnetTemplate.MainProject.Utils;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
public interface IAuthService
|
||||
{
|
||||
Task<AuthenticatedUser?> AuthenticateAsync(AuthenticateRequestData data);
|
||||
}
|
||||
|
||||
public class AuthService : BaseService, IAuthService
|
||||
{
|
||||
protected CryptUtils _cryptUtils;
|
||||
|
||||
public AuthService(
|
||||
IConfiguration configuration
|
||||
) : base(configuration)
|
||||
{
|
||||
_cryptUtils = new CryptUtils(_appSettings);
|
||||
}
|
||||
|
||||
public async Task<AuthenticatedUser?> AuthenticateAsync(AuthenticateRequestData data)
|
||||
{
|
||||
AuthenticatedUser? authenticatedUser = null;
|
||||
var decryptedUsername = _cryptUtils.Decrypt(data.Username ?? String.Empty);
|
||||
var decryptedPassword = _cryptUtils.Decrypt(data.Password ?? String.Empty);
|
||||
|
||||
if (!String.IsNullOrEmpty(decryptedUsername) && !String.IsNullOrEmpty(decryptedPassword))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return authenticatedUser;
|
||||
}
|
||||
}
|
||||
|
||||
24
MainProject/Services/BaseService.cs
Normal file
24
MainProject/Services/BaseService.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
public class BaseService
|
||||
{
|
||||
protected readonly IConfiguration _configuration;
|
||||
protected readonly AppSettings _appSettings;
|
||||
|
||||
public BaseService(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_appSettings = new AppSettings();
|
||||
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||
}
|
||||
}
|
||||
|
||||
32
MainProject/Services/JwtService.cs
Normal file
32
MainProject/Services/JwtService.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
public interface IJwtService
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class JwtService : BaseService, IJwtService
|
||||
{
|
||||
private readonly string _jwtKey;
|
||||
private readonly string _jwtIssuer;
|
||||
private readonly string _jwtAudience;
|
||||
|
||||
public JwtService(
|
||||
IConfiguration configuration
|
||||
) : base(configuration)
|
||||
{
|
||||
_jwtKey = _appSettings?.JWTSettings?.Secret ?? String.Empty;
|
||||
_jwtIssuer = _appSettings?.JWTSettings?.ValidIssuer ?? String.Empty;
|
||||
_jwtAudience = _appSettings?.JWTSettings?.ValidAudience ?? String.Empty;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user