Adding authentication and authorization flow
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.Auth;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Common.User;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Utils;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
@@ -13,12 +14,16 @@ public interface IAuthService
|
||||
public class AuthService : BaseService, IAuthService
|
||||
{
|
||||
protected CryptUtils _cryptUtils;
|
||||
protected readonly IUserService _userService;
|
||||
|
||||
public AuthService(
|
||||
IConfiguration configuration
|
||||
) : base(configuration)
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext,
|
||||
IUserService userService
|
||||
) : base(configuration, sqlServerContext)
|
||||
{
|
||||
_cryptUtils = new CryptUtils(_appSettings);
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
public async Task<AuthenticatedUser?> AuthenticateAsync(AuthenticateRequestData data)
|
||||
@@ -29,7 +34,11 @@ public class AuthService : BaseService, IAuthService
|
||||
|
||||
if (!String.IsNullOrEmpty(decryptedUsername) && !String.IsNullOrEmpty(decryptedPassword))
|
||||
{
|
||||
|
||||
var user = await this._userService.GetUserByUsernameAndPassword(decryptedUsername, decryptedPassword);
|
||||
if (user != null)
|
||||
{
|
||||
authenticatedUser = new AuthenticatedUser(user);
|
||||
}
|
||||
}
|
||||
|
||||
return authenticatedUser;
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
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.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
@@ -13,12 +7,17 @@ public class BaseService
|
||||
{
|
||||
protected readonly IConfiguration _configuration;
|
||||
protected readonly AppSettings _appSettings;
|
||||
protected readonly SqlServerContext _sqlServerContext;
|
||||
|
||||
public BaseService(IConfiguration configuration)
|
||||
public BaseService(
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext
|
||||
)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_appSettings = new AppSettings();
|
||||
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||
_sqlServerContext = sqlServerContext;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,12 +4,13 @@ using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
public interface IJwtService
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class JwtService : BaseService, IJwtService
|
||||
@@ -19,14 +20,38 @@ public class JwtService : BaseService, IJwtService
|
||||
private readonly string _jwtAudience;
|
||||
|
||||
public JwtService(
|
||||
IConfiguration configuration
|
||||
) : base(configuration)
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext
|
||||
) : base(configuration, sqlServerContext)
|
||||
{
|
||||
_jwtKey = _appSettings?.JWTSettings?.Secret ?? String.Empty;
|
||||
_jwtIssuer = _appSettings?.JWTSettings?.ValidIssuer ?? String.Empty;
|
||||
_jwtAudience = _appSettings?.JWTSettings?.ValidAudience ?? String.Empty;
|
||||
_jwtKey = _appSettings?.JwtSettings?.Secret ?? String.Empty;
|
||||
_jwtIssuer = _appSettings?.JwtSettings?.ValidIssuer ?? String.Empty;
|
||||
_jwtAudience = _appSettings?.JwtSettings?.ValidAudience ?? String.Empty;
|
||||
}
|
||||
|
||||
|
||||
public string GenerateToken(string userId, string username)
|
||||
{
|
||||
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtKey));
|
||||
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
|
||||
var expiration = _appSettings?.JwtSettings?.ExpiredAfterMinsOfInactivity ?? 15;
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Sub, userId),
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||
new Claim("userid", userId)
|
||||
};
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
_jwtIssuer,
|
||||
_jwtAudience,
|
||||
claims,
|
||||
expires: DateTime.Now.AddMinutes(expiration),
|
||||
signingCredentials: credentials);
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
59
MainProject/Services/UserService.cs
Normal file
59
MainProject/Services/UserService.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
|
||||
using System.Collections;
|
||||
using BasicDotnetTemplate.MainProject.Core.Database;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Services;
|
||||
|
||||
public interface IUserService
|
||||
{
|
||||
Task<User?> GetUserByUsernameAndPassword(string username, string password);
|
||||
}
|
||||
|
||||
public class UserService : BaseService, IUserService
|
||||
{
|
||||
|
||||
public UserService(
|
||||
IConfiguration configuration,
|
||||
SqlServerContext sqlServerContext
|
||||
) : base(configuration, sqlServerContext)
|
||||
{ }
|
||||
|
||||
private IQueryable<User> GetUsers()
|
||||
{
|
||||
return this._sqlServerContext.Users.Where(x => !x.IsDeleted);
|
||||
}
|
||||
|
||||
private IQueryable<User> GetUserByUsername(string username)
|
||||
{
|
||||
return this._sqlServerContext.Users
|
||||
.Where(x =>
|
||||
!x.IsDeleted &&
|
||||
String.Equals(x.Username, username, StringComparison.CurrentCultureIgnoreCase)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public async Task<User?> GetUserByUsernameAndPassword(string username, string password)
|
||||
{
|
||||
User? user = null;
|
||||
|
||||
try
|
||||
{
|
||||
user = await this.GetUserByUsername(username).FirstOrDefaultAsync();
|
||||
if (user != null)
|
||||
{
|
||||
var encryptedPassword = user.PasswordHash;
|
||||
Console.WriteLine(encryptedPassword);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception.Message);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user