Files
moadiran/TaxPayerFull/Layout/TaxTools.razor

219 lines
8.5 KiB
Plaintext
Raw Normal View History

2024-04-05 01:05:32 +03:30
@using Shared.DTOs
2024-04-05 17:43:11 +03:30
@using System.Text
@inject HttpClient _hc
@inject IJSRuntime JS
2024-04-05 21:03:40 +03:30
<div class="contact-info-area pb-90" id="TaxTools">
2024-04-04 18:14:01 +03:30
<div class="container">
<div class="row">
</div>
</div>
</div>
2024-04-05 21:03:40 +03:30
<div class="tp-project__area grey-bg pt-50 pb-50 fix" >
2024-04-04 18:14:01 +03:30
<div class="container">
<div class="row gx-0">
<h5 class="inner-section-subtitle">ابزار/سامانه مودیان</h5>
2024-04-05 17:43:11 +03:30
</div>
2024-04-04 18:14:01 +03:30
<div class="row">
<div class="col-12">
<div class="popular-blog-title mb-40">
2024-04-05 17:43:11 +03:30
<h4>1)ساخت فایل CSR</h4>
</div>
</div>
</div>
2024-04-04 18:14:01 +03:30
<div class="row gx-0">
2024-04-05 17:43:11 +03:30
<div class="contact-form-right-warp">
<div class="postbox__comment-form">
<EditForm EditContext="editContext" OnValidSubmit="CreateCsrAndPrivateKey">
<DataAnnotationsValidator />
<div class="postbox__comment-input mb-35">
<ValidationMessage For="()=>modelTaxTools.cn" />
<ValidationMessage For="()=>modelTaxTools.sn" />
<ValidationMessage For="()=>modelTaxTools.company" />
<ValidationMessage For="()=>modelTaxTools.Mobile" />
</div>
<div class="row gx-20">
<div class="col-12">
<div class="postbox__comment-input mb-30">
<InputText @bind-Value="modelTaxTools.cn" id="cn" type="text" class="inputText" required="" />
2024-04-04 18:14:01 +03:30
<span class="floating-label">cn</span>
</div>
2024-04-05 17:43:11 +03:30
</div>
<div class="col-12">
<div class="postbox__comment-input mb-30">
<InputText @bind-Value="modelTaxTools.sn" id="sn" type="text" class="inputText" required="" />
<span class="floating-label">sn</span>
2024-04-04 18:14:01 +03:30
</div>
2024-04-05 17:43:11 +03:30
</div>
<div class="col-12">
<div class="postbox__comment-input mb-35">
<InputText @bind-Value="modelTaxTools.company" id="company" type="text" class="inputText" required="" />
<span class="floating-label">Company</span>
2024-04-04 18:14:01 +03:30
</div>
2024-04-05 17:43:11 +03:30
</div>
2024-04-04 18:14:01 +03:30
<div class="col-12">
<div class="postbox__comment-input mb-35">
2024-04-05 17:43:11 +03:30
<InputText @bind-Value="modelTaxTools.Mobile" id="Mobile" type="text" class="inputText" required="" />
2024-04-04 18:14:01 +03:30
<span class="floating-label">Mobile</span>
</div>
</div>
<div class="col-xxl-6">
2024-04-05 17:43:11 +03:30
<div class="postbox__btn-box w-50">
<button type="submit" class="btn btn-outline-primary">ایجاد CSR و PrivateKey</button>
</div>
</div>
@* <div class="col-xxl-6">
2024-04-04 18:14:01 +03:30
<div class="postbox__btn-box">
2024-04-05 17:43:11 +03:30
<a onclick="@ReadPublicKeyFromCER" class="btn btn-outline-primary">2)خواندن PublicKey</a>
2024-04-04 18:14:01 +03:30
</div>
2024-04-05 17:43:11 +03:30
</div> *@
2024-04-04 18:14:01 +03:30
</div>
2024-04-05 17:43:11 +03:30
</EditForm>
<br /> <br />
<div class="row">
<div class="col-12">
<div class="popular-blog-title mb-20">
<h4>2)خواندن کلید عمومی</h4>
2024-04-04 18:14:01 +03:30
</div>
2024-04-05 21:03:40 +03:30
<div class="row">
<label style="color:red">
@ErrorReadpublickey
</label>
</div>
<div class="row">
2024-04-05 17:43:11 +03:30
<label>
حداکثر سایز مجاز فایل @maxFileSize بایت:
2024-04-05 21:03:40 +03:30
<InputFile OnChange="ReadPublicKeyFromCER" multiple />
2024-04-05 17:43:11 +03:30
</label>
2024-04-05 21:03:40 +03:30
</div>
2024-04-05 17:43:11 +03:30
</div>
2024-04-04 18:14:01 +03:30
</div>
2024-04-05 17:43:11 +03:30
2024-04-04 18:14:01 +03:30
</div>
2024-04-05 17:43:11 +03:30
</div>
2024-04-04 18:14:01 +03:30
</div>
</div>
</div>
@code {
2024-04-05 21:03:40 +03:30
2024-04-05 17:43:11 +03:30
private long maxFileSize = 1024 * 15;
2024-04-05 21:03:40 +03:30
public string ErrorReadpublickey { get; set; }
2024-04-05 17:43:11 +03:30
private EditContext? editContext;
[SupplyParameterFromForm]
private CsrPrivateKeyDto? modelTaxTools { get; set; } = new CsrPrivateKeyDto();
private ValidationMessageStore? messageStore;
protected override void OnInitialized()
{
// modelTaxTools ??= new();
// editContext = new(modelTaxTools);
// editContext.OnValidationRequested += HandleValidationRequested;
// messageStore = new(editContext);
editContext = new EditContext(modelTaxTools);
messageStore = new(editContext);
}
// private void HandleValidationRequested(object? sender,
// ValidationRequestedEventArgs args)
// {
// messageStore?.Clear();
// // messageStore?.Add(() => modelTaxTools.Mobile, "Select at least one.");
// }
2024-04-04 18:14:01 +03:30
}
2024-04-05 17:43:11 +03:30
@functions {
private async Task CreateCsrAndPrivateKey()
2024-04-05 01:05:32 +03:30
{
2024-04-05 17:43:11 +03:30
var request = await _hc.PostAsJsonAsync("Base/CreateCsrAndPrivateKey", modelTaxTools);
if (request.IsSuccessStatusCode)
{
2024-04-05 21:03:40 +03:30
messageStore?.Clear();
2024-04-05 17:43:11 +03:30
var res = await request.Content.ReadFromJsonAsync<TaxToolsDTO>();
await DownloadFileFromStream(res.Base64csr, $"{modelTaxTools.cn}.{res.typecsr}");
await DownloadFileFromStream(res.Base64key, $"{modelTaxTools.cn}.{res.typekey}");
}
else
{
var error = await request.Content.ReadFromJsonAsync<List<string>>();
messageStore?.Add(() => modelTaxTools.Mobile, error);
}
2024-04-05 01:05:32 +03:30
}
2024-04-05 17:43:11 +03:30
private async Task ReadPublicKeyFromCER(InputFileChangeEventArgs e)
2024-04-05 01:05:32 +03:30
{
2024-04-05 21:03:40 +03:30
ErrorReadpublickey = "";
2024-04-05 17:43:11 +03:30
if (e.GetMultipleFiles()[0].Size <= maxFileSize)
{
string Base64Str = "";
using (MemoryStream stream = new MemoryStream())
{
await e.GetMultipleFiles()[0].OpenReadStream(maxFileSize).CopyToAsync(stream);
byte[] Array = stream.ToArray();
Base64Str = Convert.ToBase64String(Array);
}
2024-04-05 01:05:32 +03:30
2024-04-05 21:03:40 +03:30
if (!string.IsNullOrEmpty(Base64Str))
{
var request = await _hc.PostAsJsonAsync("Base/ReadPublicKeyFromCER", Base64Str);
if (request.IsSuccessStatusCode)
{
var res = await request.Content.ReadFromJsonAsync<PublicKeyDTO>();
await DownloadFileFromStream(res.PublicKeyBase64, $"{modelTaxTools.cn}.{res.type}");
}
else
{
ErrorReadpublickey = "خطایی در اجرای عملیات رخ داده";
}
}
2024-04-05 17:43:11 +03:30
}
else
{
2024-04-05 21:03:40 +03:30
ErrorReadpublickey = "حجم فایل بیشتر از حد مجاز می باشد";
2024-04-05 17:43:11 +03:30
}
2024-04-05 21:03:40 +03:30
2024-04-05 17:43:11 +03:30
}
private Stream GetFileStream(byte[] bytes)
{
var fileStream = new MemoryStream(bytes);
return fileStream;
}
private async Task DownloadFileFromStream(string Base64, string FileName)
{
byte[] bytes = System.Convert.FromBase64String(Base64);
var fileStream = GetFileStream(bytes);
// var fileName = "log.bin";
using var streamRef = new DotNetStreamReference(stream: fileStream);
await JS.InvokeVoidAsync("downloadFileFromStream", FileName, streamRef);
}
}
<script>
window.downloadFileFromStream = async (fileName, contentStreamReference) => {
const arrayBuffer = await contentStreamReference.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const url = URL.createObjectURL(blob);
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
URL.revokeObjectURL(url);
2024-04-05 01:05:32 +03:30
}
2024-04-05 17:43:11 +03:30
</script>