diff --git a/Back/Back.csproj b/Back/Back.csproj index 4462681..ac6c0cd 100644 --- a/Back/Back.csproj +++ b/Back/Back.csproj @@ -26,11 +26,16 @@ - + + + ..\..\Dlls\Service.dll + + + diff --git a/Back/Controllers/BaseController.cs b/Back/Controllers/BaseController.cs index fbd374e..611f661 100644 --- a/Back/Controllers/BaseController.cs +++ b/Back/Controllers/BaseController.cs @@ -1,4 +1,5 @@ using Back.Services; +using Back.Validations; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Shared.DTOs; @@ -10,9 +11,11 @@ namespace Back.Controllers public class BaseController : ControllerBase { private readonly ServBase _sBase; - public BaseController(ServBase sBase) + private readonly MobileValidation _mobilevalidation; + public BaseController(ServBase sBase, MobileValidation mobilevalidation) { _sBase = sBase; + _mobilevalidation = mobilevalidation; } [HttpGet("Pricing")] public async Task>> Pricing() @@ -34,6 +37,17 @@ namespace Back.Controllers [HttpGet("LastQuestion")] public async Task>> LastQuestion(int PageIndex, int PageSize) => Ok(await _sBase.GetQuestion(PageIndex, PageSize)); - + [HttpPost("CreateCsrAndPrivateKey")] + public async Task> 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> ReadPublicKeyFromCER(string modelfromBase64) + => Ok(await _sBase.ReadPublicKeyFromCER(modelfromBase64)); + } } diff --git a/Back/Program.cs b/Back/Program.cs index 314735b..a262e78 100644 --- a/Back/Program.cs +++ b/Back/Program.cs @@ -1,6 +1,7 @@ using Back.Data.Contracts; using Back.Data.Infrastructure.Repository; using Back.Services; +using Back.Validations; using Microsoft.EntityFrameworkCore; using TaxPayer.Infrastructure.Persistence; @@ -18,6 +19,8 @@ builder.Services.AddDbContext(options => }); builder.Services.AddScoped(typeof(IAsyncRepository<>), typeof(RepositoryBase<>)); builder.Services.AddScoped(); +builder.Services.AddScoped (); +builder.Services.AddScoped(); string origins = "OriginTaxPayer"; builder.Services.AddCors(options => { diff --git a/Back/Services/ServBase.cs b/Back/Services/ServBase.cs index 0de2868..a761274 100644 --- a/Back/Services/ServBase.cs +++ b/Back/Services/ServBase.cs @@ -3,6 +3,8 @@ using Back.Data.Contracts; using Back.Data.Models; using Microsoft.EntityFrameworkCore; using Shared.DTOs; +using System.Diagnostics; +using System.Text; namespace Back.Services { @@ -11,11 +13,17 @@ namespace Back.Services private readonly IAsyncRepository _repoPricing; private readonly IAsyncRepository _repoBlog; private readonly IAsyncRepository _repoQuestion; - public ServBase(IAsyncRepository repoPricing, IAsyncRepository repoBlog, IAsyncRepository repoQuestion) + private readonly IAsyncRepository _repoSaleLead; + private readonly Service.Main _Taxtools; + public ServBase(IAsyncRepository repoPricing, + IAsyncRepository repoBlog, IAsyncRepository repoQuestion + , Service.Main taxtools, IAsyncRepository repoSaleLead) { _repoPricing = repoPricing; _repoBlog = repoBlog; _repoQuestion = repoQuestion; + _Taxtools = taxtools; + _repoSaleLead = repoSaleLead; } public async Task> GetBasePrice() { @@ -65,6 +73,130 @@ namespace Back.Services }).FirstOrDefaultAsync(); return result; } + public async Task CreateCsrAndPrivateKey(CsrPrivateKeyDto model) + { + TaxToolsDTO taxTools = null; + List values = new List() + { + 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 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" + }; + } } } diff --git a/Back/Validations/MobileValidation.cs b/Back/Validations/MobileValidation.cs new file mode 100644 index 0000000..407137d --- /dev/null +++ b/Back/Validations/MobileValidation.cs @@ -0,0 +1,18 @@ +using FluentValidation; +using Shared.DTOs; +using System; + +namespace Back.Validations +{ + public class MobileValidation : AbstractValidator + { + public MobileValidation() + { + RuleFor(m => m) + .NotEmpty().WithMessage("موبایل نمی تواند باشد") + .NotNull().WithMessage("موبایل نمی تواند باشد") + .Length(11).WithMessage("فرمت موبایل صحیح نمی باشد") + .Must(m => m.StartsWith("09")).WithMessage("فرمت موبایل صحیح نمی باشد"); + } + } +} diff --git a/Shared/DTOs/CsrPrivateKeyDto.cs b/Shared/DTOs/CsrPrivateKeyDto.cs new file mode 100644 index 0000000..8aa748a --- /dev/null +++ b/Shared/DTOs/CsrPrivateKeyDto.cs @@ -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; } + } +} diff --git a/Shared/DTOs/PublicKeyDTO.cs b/Shared/DTOs/PublicKeyDTO.cs new file mode 100644 index 0000000..7271fdb --- /dev/null +++ b/Shared/DTOs/PublicKeyDTO.cs @@ -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; } + } +} diff --git a/Shared/DTOs/TaxToolsDTO.cs b/Shared/DTOs/TaxToolsDTO.cs new file mode 100644 index 0000000..ad0cd3c --- /dev/null +++ b/Shared/DTOs/TaxToolsDTO.cs @@ -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; } + } +} diff --git a/TaxPayerFull/Layout/TaxTools.razor b/TaxPayerFull/Layout/TaxTools.razor index f82acf9..75bb1e8 100644 --- a/TaxPayerFull/Layout/TaxTools.razor +++ b/TaxPayerFull/Layout/TaxTools.razor @@ -1,4 +1,5 @@ -
+@using Shared.DTOs +
@@ -19,45 +20,45 @@
-
+
- + cn
- + sn
- + Company
- + Mobile
- +
- +
-
+
@@ -66,5 +67,15 @@
@code { - + private CsrPrivateKeyDto? model { get; set; } = new CsrPrivateKeyDto(); } +@functions{ + private void CreateCsrAndPrivateKey() + { + + } + private void ReadPublicKeyFromCER() + { + + } +} \ No newline at end of file