This commit is contained in:
mmrbnjd
2024-04-05 01:05:32 +03:30
parent 0b1ad82130
commit 4eb04aef14
9 changed files with 234 additions and 14 deletions

View File

@@ -26,11 +26,16 @@
<ItemGroup> <ItemGroup>
<Folder Include="Common\DTOs\" /> <Folder Include="Common\DTOs\" />
<Folder Include="Features\" /> <Folder Include="Features\" />
<Folder Include="Validations\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" /> <ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="Service">
<HintPath>..\..\Dlls\Service.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>

View File

@@ -1,4 +1,5 @@
using Back.Services; using Back.Services;
using Back.Validations;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Shared.DTOs; using Shared.DTOs;
@@ -10,9 +11,11 @@ namespace Back.Controllers
public class BaseController : ControllerBase public class BaseController : ControllerBase
{ {
private readonly ServBase _sBase; private readonly ServBase _sBase;
public BaseController(ServBase sBase) private readonly MobileValidation _mobilevalidation;
public BaseController(ServBase sBase, MobileValidation mobilevalidation)
{ {
_sBase = sBase; _sBase = sBase;
_mobilevalidation = mobilevalidation;
} }
[HttpGet("Pricing")] [HttpGet("Pricing")]
public async Task<ActionResult<List<BasePriceDto>>> Pricing() public async Task<ActionResult<List<BasePriceDto>>> Pricing()
@@ -34,6 +37,17 @@ namespace Back.Controllers
[HttpGet("LastQuestion")] [HttpGet("LastQuestion")]
public async Task<ActionResult<PagingDto<QuestionDto>>> LastQuestion(int PageIndex, int PageSize) public async Task<ActionResult<PagingDto<QuestionDto>>> LastQuestion(int PageIndex, int PageSize)
=> Ok(await _sBase.GetQuestion(PageIndex, PageSize)); => Ok(await _sBase.GetQuestion(PageIndex, PageSize));
[HttpPost("CreateCsrAndPrivateKey")]
public async Task<ActionResult<TaxToolsDTO>> CreateCsrAndPrivateKey(CsrPrivateKeyDto model)
{
var resultValidationmodel = await _mobilevalidation.ValidateAsync(model.Mobile);
if (!resultValidationmodel.IsValid)
return BadRequest(resultValidationmodel.Errors);
return Ok(await _sBase.CreateCsrAndPrivateKey(model));
}
[HttpPost("ReadPublicKeyFromCER")]
public async Task<ActionResult<PublicKeyDTO>> ReadPublicKeyFromCER(string modelfromBase64)
=> Ok(await _sBase.ReadPublicKeyFromCER(modelfromBase64));
} }
} }

View File

@@ -1,6 +1,7 @@
using Back.Data.Contracts; using Back.Data.Contracts;
using Back.Data.Infrastructure.Repository; using Back.Data.Infrastructure.Repository;
using Back.Services; using Back.Services;
using Back.Validations;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using TaxPayer.Infrastructure.Persistence; using TaxPayer.Infrastructure.Persistence;
@@ -18,6 +19,8 @@ builder.Services.AddDbContext<SqlDbContext>(options =>
}); });
builder.Services.AddScoped(typeof(IAsyncRepository<>), typeof(RepositoryBase<>)); builder.Services.AddScoped(typeof(IAsyncRepository<>), typeof(RepositoryBase<>));
builder.Services.AddScoped<Back.Services.ServBase>(); builder.Services.AddScoped<Back.Services.ServBase>();
builder.Services.AddScoped<MobileValidation> ();
builder.Services.AddScoped<Service.Main>();
string origins = "OriginTaxPayer"; string origins = "OriginTaxPayer";
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {

View File

@@ -3,6 +3,8 @@ using Back.Data.Contracts;
using Back.Data.Models; using Back.Data.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Shared.DTOs; using Shared.DTOs;
using System.Diagnostics;
using System.Text;
namespace Back.Services namespace Back.Services
{ {
@@ -11,11 +13,17 @@ namespace Back.Services
private readonly IAsyncRepository<Pricing> _repoPricing; private readonly IAsyncRepository<Pricing> _repoPricing;
private readonly IAsyncRepository<Blog> _repoBlog; private readonly IAsyncRepository<Blog> _repoBlog;
private readonly IAsyncRepository<Question> _repoQuestion; private readonly IAsyncRepository<Question> _repoQuestion;
public ServBase(IAsyncRepository<Pricing> repoPricing, IAsyncRepository<Blog> repoBlog, IAsyncRepository<Question> repoQuestion) private readonly IAsyncRepository<SaleLead> _repoSaleLead;
private readonly Service.Main _Taxtools;
public ServBase(IAsyncRepository<Pricing> repoPricing,
IAsyncRepository<Blog> repoBlog, IAsyncRepository<Question> repoQuestion
, Service.Main taxtools, IAsyncRepository<SaleLead> repoSaleLead)
{ {
_repoPricing = repoPricing; _repoPricing = repoPricing;
_repoBlog = repoBlog; _repoBlog = repoBlog;
_repoQuestion = repoQuestion; _repoQuestion = repoQuestion;
_Taxtools = taxtools;
_repoSaleLead = repoSaleLead;
} }
public async Task<List<BasePriceDto>> GetBasePrice() public async Task<List<BasePriceDto>> GetBasePrice()
{ {
@@ -65,6 +73,130 @@ namespace Back.Services
}).FirstOrDefaultAsync(); }).FirstOrDefaultAsync();
return result; return result;
} }
public async Task<TaxToolsDTO?> CreateCsrAndPrivateKey(CsrPrivateKeyDto model)
{
TaxToolsDTO taxTools = null;
List<Service.PrmValue> values = new List<Service.PrmValue>()
{
new Service.PrmValue(){Prm="CN",Value=model.cn},
new Service.PrmValue(){Prm="serialNumber",Value=model.sn},
new Service.PrmValue(){Prm="O",Value="Non-Governmental"},
new Service.PrmValue(){Prm="3.OU",Value=model.company},
new Service.PrmValue(){Prm="2.OU",Value=model.company},
new Service.PrmValue(){Prm="1.OU",Value=model.company},
new Service.PrmValue(){Prm="C",Value="IR"},
};
string msg = "";
if (_Taxtools.CraeteCnfFile(values, ref msg))
{
if (_Taxtools.CreateCsrAndPrivateKey(model.cn, ref msg))
{
taxTools = new TaxToolsDTO();
//خواندن
Stream stream = File.Open($"C:\\OpenSSL\\bin\\{model.cn}.Csr", FileMode.Open);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
string csr = Encoding.UTF8.GetString(buffer);
stream.Flush();
stream.Close();
taxTools.csr = csr;
taxTools.Base64csr = Convert.ToBase64String(buffer);
taxTools.typecsr = "Csr";
stream = File.Open($"C:\\OpenSSL\\bin\\{model.cn}.key", FileMode.Open);
buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
string key = Encoding.UTF8.GetString(buffer);
taxTools.key = key;
taxTools.Base64key = Convert.ToBase64String(buffer);
taxTools.typekey = "key";
stream.Flush();
stream.Close();
//_contextMongodb.InsertItem(new SysLog()
//{
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/CreateCsrAndPrivateKey",
// Value = JsonConvert.SerializeObject(model),
// Route = _httpContextAccessor.HttpContext.Request.Path,
// Type = "User"
//});
await _repoSaleLead.AddAsync(new SaleLead
{
cn = model.cn,
company = model.company,
csr = csr,
Key = key,
Mobile = model.Mobile,
sn = model.sn
});
}
}
if (string.IsNullOrEmpty(msg))
{
//_contextMongodb.InsertItem(new SysLog()
//{
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/CreateCsrAndPrivateKey",
// Value = msg,
// Route = _httpContextAccessor.HttpContext.Request.Path,
// Type = "ERROR"
//});
}
return taxTools;
}
public async Task<PublicKeyDTO> ReadPublicKeyFromCER(string modelfromBase64)
{
string name = $"{DateTime.Now.Year}{DateTime.Now.Month}{DateTime.Now.DayOfWeek}{DateTime.Now.Hour}{DateTime.Now.Minute}{DateTime.Now.Second}";
string filePath = $"C:\\cer\\{name}.cer";
Stream stream = System.IO.File.Open(filePath, FileMode.Create);
var encoding = Convert.FromBase64String(modelfromBase64);
stream.Write(encoding, 0, encoding.Length);
stream.Flush();
stream.Close();
Process cmd = new Process();
cmd.StartInfo.WorkingDirectory = Environment.CurrentDirectory + "C\\OpenSSL\\bin";
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine($"openssl x509 -pubkey -noout -in {filePath}> {name}.txt");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
//خواندن
stream = System.IO.File.Open($"C\\OpenSSL\\bin\\{name}.txt", FileMode.Open);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
string PublicKey = Encoding.UTF8.GetString(buffer);
stream.Flush();
stream.Close();
//_contextMongodb.InsertItem(new SysLog()
//{
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/ReadPublicKeyFromCER",
// Value = modelfromBase64,
// Route = _httpContextAccessor.HttpContext.Request.Path,
// Type = "User"
//});
return new PublicKeyDTO
{
PublicKey = PublicKey,
PublicKeyBase64 = Convert.ToBase64String(buffer),
type = "txt"
};
}
} }
} }

View File

@@ -0,0 +1,18 @@
using FluentValidation;
using Shared.DTOs;
using System;
namespace Back.Validations
{
public class MobileValidation : AbstractValidator<string>
{
public MobileValidation()
{
RuleFor(m => m)
.NotEmpty().WithMessage("موبایل نمی تواند باشد")
.NotNull().WithMessage("موبایل نمی تواند باشد")
.Length(11).WithMessage("فرمت موبایل صحیح نمی باشد")
.Must(m => m.StartsWith("09")).WithMessage("فرمت موبایل صحیح نمی باشد");
}
}
}

View File

@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
namespace Shared.DTOs
{
public class CsrPrivateKeyDto
{
[Required]
public string cn { get; set; }
[Required]
public string sn { get; set; }
[Required]
public string company { get; set; }
[Required]
public string Mobile { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
namespace Shared.DTOs
{
public class PublicKeyDTO
{
public string PublicKey { get; set; }
public string PublicKeyBase64 { get; set; }
public string type { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
namespace Shared.DTOs
{
public class TaxToolsDTO
{
public string csr { get; set; }
public string Base64csr { get; set; }
public string typecsr { get; set; }
public string key { get; set; }
public string Base64key { get; set; }
public string typekey { get; set; }
}
}

View File

@@ -1,4 +1,5 @@
<div class="contact-info-area pb-90" id="contact"> @using Shared.DTOs
<div class="contact-info-area pb-90" id="contact">
<div class="container"> <div class="container">
<div class="row"> <div class="row">
</div> </div>
@@ -19,45 +20,45 @@
<div class="row gx-0"> <div class="row gx-0">
<div class="contact-form-right-warp"> <div class="contact-form-right-warp">
<div class="postbox__comment-form"> <div class="postbox__comment-form">
<form action="#" class="box">
<div class="row gx-20"> <div class="row gx-20">
<div class="col-12"> <div class="col-12">
<div class="postbox__comment-input mb-30"> <div class="postbox__comment-input mb-30">
<input type="text" class="inputText" required=""> <input @bind="model.cn" type="text" class="inputText" required="">
<span class="floating-label">cn</span> <span class="floating-label">cn</span>
</div> </div>
</div> </div>
<div class="col-12"> <div class="col-12">
<div class="postbox__comment-input mb-30"> <div class="postbox__comment-input mb-30">
<input type="text" class="inputText" required=""> <input @bind="model.sn" type="text" class="inputText" required="">
<span class="floating-label">sn</span> <span class="floating-label">sn</span>
</div> </div>
</div> </div>
<div class="col-12"> <div class="col-12">
<div class="postbox__comment-input mb-35"> <div class="postbox__comment-input mb-35">
<input type="text" class="inputText" required=""> <input @bind="model.company" type="text" class="inputText" required="">
<span class="floating-label">Company</span> <span class="floating-label">Company</span>
</div> </div>
</div> </div>
<div class="col-12"> <div class="col-12">
<div class="postbox__comment-input mb-35"> <div class="postbox__comment-input mb-35">
<input type="text" class="inputText" required=""> <input @bind="model.Mobile" type="text" class="inputText" required="">
<span class="floating-label">Mobile</span> <span class="floating-label">Mobile</span>
</div> </div>
</div> </div>
<div class="col-xxl-6"> <div class="col-xxl-6">
<div class="postbox__btn-box w-50"> <div class="postbox__btn-box w-50">
<button class="submit-btn">ایجاد CSR و PrivateKey</button> <button onclick="@CreateCsrAndPrivateKey" class="btn btn-outline-primary">ایجاد CSR و PrivateKey</button>
</div> </div>
</div> </div>
<div class="col-xxl-6"> <div class="col-xxl-6">
<div class="postbox__btn-box"> <div class="postbox__btn-box">
<button class="submit-btn">خواندن PublicKey</button> <button onclick="@ReadPublicKeyFromCER" class="btn btn-outline-primary">خواندن PublicKey</button>
</div> </div>
</div> </div>
</div> </div>
</form>
</div> </div>
</div> </div>
@@ -66,5 +67,15 @@
</div> </div>
@code { @code {
private CsrPrivateKeyDto? model { get; set; } = new CsrPrivateKeyDto();
} }
@functions{
private void CreateCsrAndPrivateKey()
{
}
private void ReadPublicKeyFromCER()
{
}
}