Просмотр исходного кода

forbid login after 5 unsuccessful attends

feature/test
Dzenis Hadzifejzovic 3 лет назад
Родитель
Сommit
947401c98d

+ 1
- 1
Diligent.WebAPI.Business/Services/Interfaces/IUserService.cs Просмотреть файл

{ {
public interface IUserService public interface IUserService
{ {
Task<AuthenticateResponseDto?> Authenticate(AuthenticateRequestDto model);
Task<ServiceResponseDTO<AuthenticateResponseDto>> Authenticate(AuthenticateRequestDto model);


Task<IEnumerable<User?>> GetAll(); Task<IEnumerable<User?>> GetAll();



+ 43
- 5
Diligent.WebAPI.Business/Services/UserService.cs Просмотреть файл

await _userManager.CreateAsync(user, model.Password); await _userManager.CreateAsync(user, model.Password);
} }


public async Task<AuthenticateResponseDto?> Authenticate(AuthenticateRequestDto model)
public async Task<ServiceResponseDTO<AuthenticateResponseDto>> Authenticate(AuthenticateRequestDto model)
{ {
var user = await _userManager.FindByNameAsync(model.Username); var user = await _userManager.FindByNameAsync(model.Username);


// return null if user not found // return null if user not found
if (user == null)
return null;
if (user == null)
{
return new ServiceResponseDTO<AuthenticateResponseDto>
{
IsError = true,
ErrorMessage = "Username is not valid"
};
}

var isLocked = await _userManager.IsLockedOutAsync(user);

if (isLocked)
return new ServiceResponseDTO<AuthenticateResponseDto>
{
IsError = true,
ErrorMessage = "The account is locked out"
};


var result = await _userManager.CheckPasswordAsync(user, model.Password); var result = await _userManager.CheckPasswordAsync(user, model.Password);


// password is not correct
if (!result) if (!result)
return null;
{
await _userManager.AccessFailedAsync(user);
isLocked = await _userManager.IsLockedOutAsync(user);
if(isLocked)
return new ServiceResponseDTO<AuthenticateResponseDto>
{
IsError = true,
ErrorMessage = "The account is locked out"
};

return new ServiceResponseDTO<AuthenticateResponseDto>
{
IsError = true,
ErrorMessage = "Password is not correct"
};
}


// authentication successful so generate jwt token // authentication successful so generate jwt token
var token = GenerateJwtToken(user); var token = GenerateJwtToken(user);


return new AuthenticateResponseDto
var data = new AuthenticateResponseDto
{ {
Id = user.Id, Id = user.Id,
Username = user.UserName, Username = user.UserName,
LastName = user.LastName, LastName = user.LastName,
Token = token Token = token
}; };

return new ServiceResponseDTO<AuthenticateResponseDto>
{
Data = data
};
} }


private string GenerateJwtToken(User user) private string GenerateJwtToken(User user)

+ 1
- 0
Diligent.WebAPI.Business/Usings.cs Просмотреть файл

global using Diligent.WebAPI.Contracts.DTOs.WebhookDefinition; global using Diligent.WebAPI.Contracts.DTOs.WebhookDefinition;
global using Diligent.WebAPI.Contracts.DTOs.WebhookSubscription; global using Diligent.WebAPI.Contracts.DTOs.WebhookSubscription;
global using Diligent.WebAPI.Contracts.DTOs.Auth; global using Diligent.WebAPI.Contracts.DTOs.Auth;
global using Diligent.WebAPI.Contracts.DTOs;
global using Diligent.WebAPI.Contracts.Exceptions; global using Diligent.WebAPI.Contracts.Exceptions;


global using Microsoft.EntityFrameworkCore; global using Microsoft.EntityFrameworkCore;

+ 15
- 0
Diligent.WebAPI.Contracts/DTOs/ServiceResponseDTO.cs Просмотреть файл

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diligent.WebAPI.Contracts.DTOs
{
public class ServiceResponseDTO<T> where T : class
{
public bool IsError { get; set; } = false;
public string ErrorMessage { get; set; }
public T Data { get; set; }
}
}

+ 3
- 3
Diligent.WebAPI.Host/Controllers/V1/UsersController.cs Просмотреть файл

{ {
var response = await _userService.Authenticate(model); var response = await _userService.Authenticate(model);


if (response == null)
return BadRequest(new { message = "Username or password is incorrect" });
if (response.IsError is true)
return BadRequest(new { message = response.ErrorMessage });


return Ok(response);
return Ok(response.Data);
} }
} }
} }

+ 8
- 4
Diligent.WebAPI.Host/Extensions/IdentityConfigurationExtension.cs Просмотреть файл

{ {
IServiceCollection services = builder.Services; IServiceCollection services = builder.Services;


services.AddIdentity<User, AppRole>()
.AddRoles<AppRole>()
.AddEntityFrameworkStores<DatabaseContext>()
.AddDefaultTokenProviders();
services.AddIdentity<User, AppRole>(opt =>
{
opt.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
opt.Lockout.MaxFailedAccessAttempts = 5;
})
.AddRoles<AppRole>()
.AddEntityFrameworkStores<DatabaseContext>()
.AddDefaultTokenProviders();
} }
} }
} }

Загрузка…
Отмена
Сохранить