first commit
This commit is contained in:
22
Complex.Application/Other/Common/Dto/CommonRequestDto.cs
Normal file
22
Complex.Application/Other/Common/Dto/CommonRequestDto.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
namespace Complex.Application.Common
|
||||
{
|
||||
public class CommonRequestDto
|
||||
{
|
||||
/// <summary>
|
||||
/// فیلد ترتیب
|
||||
/// </summary>
|
||||
public string? SortField { set; get; }
|
||||
/// <summary>
|
||||
/// نحوه ترتیب
|
||||
/// </summary>
|
||||
public SortDirection? sortDirection { set; get; } = SortDirection.asc;
|
||||
|
||||
public int Page { set; get; } = 0;
|
||||
public int Length { set; get; } = 10;
|
||||
}
|
||||
public enum SortDirection
|
||||
{
|
||||
asc,
|
||||
desc
|
||||
}
|
||||
}
|
||||
7
Complex.Application/Other/Common/Dto/IBaseResponseDto.cs
Normal file
7
Complex.Application/Other/Common/Dto/IBaseResponseDto.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Complex.Application.Common
|
||||
{
|
||||
public interface IBaseResponseDto
|
||||
{
|
||||
List<string> Action { set; get; }
|
||||
}
|
||||
}
|
||||
26
Complex.Application/Other/Common/Dto/ResultDto.cs
Normal file
26
Complex.Application/Other/Common/Dto/ResultDto.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace Complex.Common.Dto
|
||||
{
|
||||
public class ResultDto
|
||||
{
|
||||
public bool IsSuccess { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
|
||||
public class ResultDto<T>
|
||||
{
|
||||
public ResultDto()
|
||||
{
|
||||
|
||||
}
|
||||
public ResultDto(T data,bool isSuccess=true)
|
||||
{
|
||||
Data = data;
|
||||
IsSuccess = isSuccess;
|
||||
}
|
||||
public bool IsSuccess { get; set; }
|
||||
public string Message { get; set; }
|
||||
public T Data { get; set; }
|
||||
public int RowCount { set; get; }
|
||||
}
|
||||
|
||||
}
|
||||
71
Complex.Application/Other/Common/EnumHelpers.cs
Normal file
71
Complex.Application/Other/Common/EnumHelpers.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Complex.Common
|
||||
{
|
||||
public static class EnumHelpers<T>
|
||||
{
|
||||
public static IList<T> GetValues(Enum value)
|
||||
{
|
||||
var enumValues = new List<T>();
|
||||
|
||||
foreach (FieldInfo fi in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
|
||||
{
|
||||
enumValues.Add((T)Enum.Parse(value.GetType(), fi.Name, false));
|
||||
}
|
||||
return enumValues;
|
||||
}
|
||||
|
||||
public static T Parse(string value)
|
||||
{
|
||||
return (T)Enum.Parse(typeof(T), value, true);
|
||||
}
|
||||
|
||||
public static IList<string> GetNames(Enum value)
|
||||
{
|
||||
return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
|
||||
}
|
||||
|
||||
public static IList<string> GetDisplayValues(Enum value)
|
||||
{
|
||||
return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList();
|
||||
}
|
||||
|
||||
private static string lookupResource(Type resourceManagerProvider, string resourceKey)
|
||||
{
|
||||
foreach (PropertyInfo staticProperty in resourceManagerProvider.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
|
||||
{
|
||||
if (staticProperty.PropertyType == typeof(System.Resources.ResourceManager))
|
||||
{
|
||||
System.Resources.ResourceManager resourceManager = (System.Resources.ResourceManager)staticProperty.GetValue(null, null);
|
||||
return resourceManager.GetString(resourceKey);
|
||||
}
|
||||
}
|
||||
|
||||
return resourceKey; // Fallback with the key name
|
||||
}
|
||||
|
||||
public static string GetDisplayValue(T value)
|
||||
{
|
||||
try
|
||||
{
|
||||
var fieldInfo = value.GetType().GetField(value.ToString());
|
||||
|
||||
var descriptionAttributes = fieldInfo.GetCustomAttributes(
|
||||
typeof(DisplayAttribute), false) as DisplayAttribute[];
|
||||
|
||||
if (descriptionAttributes[0].ResourceType != null)
|
||||
return lookupResource(descriptionAttributes[0].ResourceType, descriptionAttributes[0].Name);
|
||||
|
||||
if (descriptionAttributes == null) return string.Empty;
|
||||
return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
return "نا مشخص";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
81
Complex.Application/Other/Common/FileIO.cs
Normal file
81
Complex.Application/Other/Common/FileIO.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Complex.Application.Common
|
||||
{
|
||||
public class FileIO
|
||||
{
|
||||
//==============================================================
|
||||
|
||||
public FileIO()
|
||||
{
|
||||
//
|
||||
// TODO: Add constructor logic here
|
||||
//
|
||||
}
|
||||
//==============================================================
|
||||
|
||||
/// <summary>
|
||||
/// ثبت محتوا در یک فایل
|
||||
/// </summary>
|
||||
/// <param name="fileName">نام فایل</param>
|
||||
/// <param name="content">محتوای فایل</param>
|
||||
public static void Write(string fileName, string content, bool activeInServer = false)
|
||||
{
|
||||
//if (!SystemDefine.IsLocal && !activeInServer)
|
||||
// return;
|
||||
//System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("/FileIO/" + fileName), content, Encoding.UTF8);
|
||||
Writeln(fileName, content, activeInServer);
|
||||
}
|
||||
//==============================================================
|
||||
|
||||
public static void Clear(string fileName)
|
||||
{
|
||||
//return;
|
||||
System.IO.File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "/FileIO/" + fileName, "", Encoding.UTF8);
|
||||
}
|
||||
//==============================================================
|
||||
|
||||
/// <summary>
|
||||
/// ثبت محتوا در یک فایل در یک خط جدید
|
||||
/// </summary>
|
||||
/// <param name="fileName">نام فایل</param>
|
||||
/// <param name="content">محتوای فایل</param>
|
||||
public static void Writeln(string fileName, string content, bool activeInServer = false)
|
||||
{
|
||||
|
||||
//try
|
||||
{
|
||||
|
||||
System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "/FileIO/" + fileName, content + "\r\n", Encoding.UTF8);
|
||||
}
|
||||
//catch { }
|
||||
}
|
||||
//==============================================================
|
||||
|
||||
/// <summary>
|
||||
/// ثبت محتوا در یک فایل در یک خط جدید
|
||||
/// </summary>
|
||||
/// <param name="fileName">نام فایل</param>
|
||||
/// <param name="content">محتوای فایل</param>
|
||||
public static void Writeln(string fileName, string description, Exception exc, bool activeInServer = false)
|
||||
{
|
||||
//if (!SystemDefine.IsLocal && !activeInServer)
|
||||
// return;
|
||||
|
||||
try
|
||||
{
|
||||
Writeln(fileName, ">>>>>>");
|
||||
Writeln(fileName, description);
|
||||
while (exc != null)
|
||||
{
|
||||
Writeln(fileName, exc.Message);
|
||||
Writeln(fileName, exc.StackTrace);
|
||||
exc = exc.InnerException;
|
||||
}
|
||||
Writeln(fileName, "<<<<<<");
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
//==============================================================
|
||||
}
|
||||
}
|
||||
44
Complex.Application/Other/Common/Pagination.cs
Normal file
44
Complex.Application/Other/Common/Pagination.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Linq.Dynamic.Core;
|
||||
|
||||
namespace Complex.Application.Common
|
||||
{
|
||||
public static class Pagination
|
||||
{
|
||||
public static IQueryable<TSource> ToPagedByOrder<TSource>(this IQueryable<TSource> source,
|
||||
CommonRequestDto commonRequest, out int rowsCount)
|
||||
{
|
||||
if (commonRequest.Length < 0)
|
||||
commonRequest.Length = 20000;
|
||||
if (commonRequest.Length == 0)
|
||||
commonRequest.Length = 10;
|
||||
|
||||
rowsCount = source.Count();
|
||||
|
||||
if (commonRequest.sortDirection == SortDirection.desc)
|
||||
commonRequest.SortField += " DESC";
|
||||
|
||||
source = source.OrderBy(commonRequest.SortField);
|
||||
|
||||
return source.Skip(commonRequest.Page * commonRequest.Length).Take(commonRequest.Length);
|
||||
}
|
||||
public static IQueryable<TSource> ToPaged<TSource>(this IQueryable<TSource> source, int page, int pageSize, out int rowsCount)
|
||||
{
|
||||
if (pageSize == -1)
|
||||
pageSize = 20000;
|
||||
if (pageSize == 0)
|
||||
pageSize = 10;
|
||||
|
||||
rowsCount = source.Count();
|
||||
return source.Skip(page * pageSize).Take(pageSize);
|
||||
}
|
||||
public static IQueryable<TSource> ToPaged<TSource>(this IQueryable<TSource> source, int page, int pageSize)
|
||||
{
|
||||
if (pageSize == -1)
|
||||
pageSize = 20000;
|
||||
if (pageSize == 0)
|
||||
pageSize = 10;
|
||||
|
||||
return source.Skip(page * pageSize).Take(pageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
238
Complex.Application/Other/Common/PasswordHasher.cs
Normal file
238
Complex.Application/Other/Common/PasswordHasher.cs
Normal file
@@ -0,0 +1,238 @@
|
||||
//using Microsoft.AspNetCore.Cryptography.KeyDerivation;
|
||||
//using System;
|
||||
//using System.Runtime.CompilerServices;
|
||||
//using System.Security.Cryptography;
|
||||
//using System.Text;
|
||||
|
||||
//namespace Complex.Common
|
||||
//{
|
||||
// public class PasswordHasher
|
||||
// {
|
||||
// // Format Markers:
|
||||
// // IdentityV2: PBKDF2 with HMAC-SHA1, 128-bit salt, 256-bit subkey, 1000 iterations.
|
||||
// // IdentityV2 Format: { 0x00(byte), salt, subkey }
|
||||
// // IdentityV3: PBKDF2 with HMAC-SHA256, 128-bit salt, 256-bit subkey, 10000 iterations.
|
||||
// // IdentityV3 Format: { 0x01(byte), prf(UInt32), iter count(UInt32), salt length(UInt32), salt, subkey }
|
||||
// // Custom = PBKDF2 with custom configuration.
|
||||
// // Format: { 0xC0(byte), salt, subkey } OR
|
||||
// // { 0xC0(byte), prf(UInt32), iter count(UInt32), salt length(UInt32), salt, subkey }
|
||||
// // Header Info: _includeHeaderInfo
|
||||
// // IdentityV3 includes configuration in the header, IdentityV2 does not.
|
||||
// // Use AspNetCore: _useAspNetCore
|
||||
// // Microsoft.AspNetCore.Cryptography.KeyDerivation is required
|
||||
// // else use System.Security.Cryptography
|
||||
|
||||
// private readonly bool _useAspNetCore;
|
||||
// private readonly byte _formatMarker;
|
||||
// private readonly KeyDerivationPrf _prf; // Requires Microsoft.AspNetCore
|
||||
// private readonly HashAlgorithmName _hashAlgorithmName;
|
||||
// private readonly bool _includeHeaderInfo;
|
||||
// private readonly int _saltLength;
|
||||
// private readonly int _requestedLength;
|
||||
// private readonly int _iterCount;
|
||||
|
||||
// public PasswordHasher()
|
||||
// {
|
||||
// _useAspNetCore = true;
|
||||
|
||||
// // IdentityV2
|
||||
// //_formatMarker = 0x00;
|
||||
// //_prf = KeyDerivationPrf.HMACSHA1; // Requires Microsoft.AspNetCore
|
||||
// //_hashAlgorithmName = HashAlgorithmName.SHA1;
|
||||
// //_includeHeaderInfo = false;
|
||||
// //_saltLength = 128 / 8; // bits/1 byte = 16
|
||||
// //_requestedLength = 256 / 8; // bits/1 byte = 32
|
||||
// //_iterCount = 1000;
|
||||
|
||||
// // IdentityV3
|
||||
// _formatMarker = 0x01;
|
||||
// _prf = KeyDerivationPrf.HMACSHA256; // Requires Microsoft.AspNetCore
|
||||
// _hashAlgorithmName = HashAlgorithmName.SHA256;
|
||||
// _includeHeaderInfo = true;
|
||||
// _saltLength = 128 / 8; // bits/1 byte = 16
|
||||
// _requestedLength = 256 / 8; // bits/1 byte = 32
|
||||
// _iterCount = 10000;
|
||||
|
||||
// // Custom Max
|
||||
// //_formatMarker = 0xC0;
|
||||
// //_prf = KeyDerivationPrf.HMACSHA512; // Requires Microsoft.AspNetCore
|
||||
// //_hashAlgorithmName = HashAlgorithmName.SHA512;
|
||||
// //_includeHeaderInfo = true;
|
||||
// //_saltLength = 512 / 8; // bits/1 byte = 64
|
||||
// //_requestedLength = 512 / 8; // bits/1 byte = 64
|
||||
// //_iterCount = 100000;
|
||||
// }
|
||||
|
||||
// public string HashPassword(string password)
|
||||
// {
|
||||
// if (string.IsNullOrEmpty(password)) throw new ArgumentNullException(nameof(password));
|
||||
|
||||
// byte[] salt = new byte[_saltLength];
|
||||
// using (var rng = RandomNumberGenerator.Create())
|
||||
// {
|
||||
// rng.GetBytes(salt);
|
||||
// }
|
||||
|
||||
// byte[] subkey = new byte[_requestedLength];
|
||||
// if (_useAspNetCore)
|
||||
// {
|
||||
// subkey = KeyDerivation.Pbkdf2(password, salt, _prf, _iterCount, _requestedLength);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// using var pbkdf2 = new Rfc2898DeriveBytes(password, salt, _iterCount, _hashAlgorithmName);
|
||||
// subkey = pbkdf2.GetBytes(_requestedLength);
|
||||
// }
|
||||
|
||||
// var headerByteLength = 1; // Format marker only
|
||||
// if (_includeHeaderInfo) headerByteLength = 13;
|
||||
|
||||
// var outputBytes = new byte[headerByteLength + salt.Length + subkey.Length];
|
||||
|
||||
// outputBytes[0] = (byte)_formatMarker;
|
||||
|
||||
// if (_includeHeaderInfo)
|
||||
// {
|
||||
// if (_useAspNetCore)
|
||||
// {
|
||||
// WriteNetworkByteOrder(outputBytes, 1, (uint)_prf);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var shaInt = 1;
|
||||
// if (_hashAlgorithmName == HashAlgorithmName.SHA1) shaInt = 0;
|
||||
// else if (_hashAlgorithmName == HashAlgorithmName.SHA256) shaInt = 1;
|
||||
// else if (_hashAlgorithmName == HashAlgorithmName.SHA512) shaInt = 2;
|
||||
|
||||
// WriteNetworkByteOrder(outputBytes, 1, (uint)shaInt);
|
||||
// }
|
||||
|
||||
// WriteNetworkByteOrder(outputBytes, 5, (uint)_iterCount);
|
||||
// WriteNetworkByteOrder(outputBytes, 9, (uint)_saltLength);
|
||||
// }
|
||||
|
||||
// Buffer.BlockCopy(salt, 0, outputBytes, headerByteLength, salt.Length);
|
||||
// Buffer.BlockCopy(subkey, 0, outputBytes, headerByteLength + _saltLength, subkey.Length);
|
||||
|
||||
// return Convert.ToBase64String(outputBytes);
|
||||
// }
|
||||
|
||||
// public bool VerifyPassword(string hashedPassword, string enteredPassword)
|
||||
// {
|
||||
// if (string.IsNullOrEmpty(enteredPassword) || string.IsNullOrEmpty(hashedPassword)) return false;
|
||||
|
||||
// byte[] decodedHashedPassword;
|
||||
// try
|
||||
// {
|
||||
// decodedHashedPassword = Convert.FromBase64String(hashedPassword);
|
||||
// }
|
||||
// catch (Exception)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// if (decodedHashedPassword.Length == 0) return false;
|
||||
|
||||
// // Read the format marker
|
||||
// var verifyMarker = (byte)decodedHashedPassword[0];
|
||||
// if (_formatMarker != verifyMarker) return false;
|
||||
|
||||
// try
|
||||
// {
|
||||
// if (_includeHeaderInfo)
|
||||
// {
|
||||
// // Read header information
|
||||
// var shaUInt = (uint)ReadNetworkByteOrder(decodedHashedPassword, 1);
|
||||
// var verifyPrf = shaUInt switch
|
||||
// {
|
||||
// 0 => KeyDerivationPrf.HMACSHA1,
|
||||
// 1 => KeyDerivationPrf.HMACSHA256,
|
||||
// 2 => KeyDerivationPrf.HMACSHA512,
|
||||
// _ => KeyDerivationPrf.HMACSHA256,
|
||||
// };
|
||||
// if (_prf != verifyPrf) return false;
|
||||
|
||||
// var verifyAlgorithmName = shaUInt switch
|
||||
// {
|
||||
// 0 => HashAlgorithmName.SHA1,
|
||||
// 1 => HashAlgorithmName.SHA256,
|
||||
// 2 => HashAlgorithmName.SHA512,
|
||||
// _ => HashAlgorithmName.SHA256,
|
||||
// };
|
||||
// if (_hashAlgorithmName != verifyAlgorithmName) return false;
|
||||
|
||||
// int iterCountRead = (int)ReadNetworkByteOrder(decodedHashedPassword, 5);
|
||||
// if (_iterCount != iterCountRead) return false;
|
||||
|
||||
// int saltLengthRead = (int)ReadNetworkByteOrder(decodedHashedPassword, 9);
|
||||
// if (_saltLength != saltLengthRead) return false;
|
||||
// }
|
||||
|
||||
// var headerByteLength = 1; // Format marker only
|
||||
// if (_includeHeaderInfo) headerByteLength = 13;
|
||||
|
||||
// // Read the salt
|
||||
// byte[] salt = new byte[_saltLength];
|
||||
// Buffer.BlockCopy(decodedHashedPassword, headerByteLength, salt, 0, salt.Length);
|
||||
|
||||
// // Read the subkey (the rest of the payload)
|
||||
// int subkeyLength = decodedHashedPassword.Length - headerByteLength - salt.Length;
|
||||
|
||||
// if (_requestedLength != subkeyLength) return false;
|
||||
|
||||
// byte[] expectedSubkey = new byte[subkeyLength];
|
||||
// Buffer.BlockCopy(decodedHashedPassword, headerByteLength + salt.Length, expectedSubkey, 0, expectedSubkey.Length);
|
||||
|
||||
// // Hash the incoming password and verify it
|
||||
// byte[] actualSubkey = new byte[_requestedLength];
|
||||
// if (_useAspNetCore)
|
||||
// {
|
||||
// actualSubkey = KeyDerivation.Pbkdf2(enteredPassword, salt, _prf, _iterCount, subkeyLength);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// using var pbkdf2 = new Rfc2898DeriveBytes(enteredPassword, salt, _iterCount, _hashAlgorithmName);
|
||||
// actualSubkey = pbkdf2.GetBytes(_requestedLength);
|
||||
// }
|
||||
|
||||
// return ByteArraysEqual(actualSubkey, expectedSubkey);
|
||||
// }
|
||||
// catch
|
||||
// {
|
||||
// // This should never occur except in the case of a malformed payload, where
|
||||
// // we might go off the end of the array. Regardless, a malformed payload
|
||||
// // implies verification failed.
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Compares two byte arrays for equality. The method is specifically written so that the loop is not optimized.
|
||||
// [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
|
||||
// private static bool ByteArraysEqual(byte[] a, byte[] b)
|
||||
// {
|
||||
// if (a == null && b == null) return true;
|
||||
// if (a == null || b == null || a.Length != b.Length) return false;
|
||||
// var areSame = true;
|
||||
// for (var i = 0; i < a.Length; i++) { areSame &= (a[i] == b[i]); }
|
||||
// return areSame;
|
||||
// }
|
||||
|
||||
// private static uint ReadNetworkByteOrder(byte[] buffer, int offset)
|
||||
// {
|
||||
// return ((uint)(buffer[offset + 0]) << 24)
|
||||
// | ((uint)(buffer[offset + 1]) << 16)
|
||||
// | ((uint)(buffer[offset + 2]) << 8)
|
||||
// | ((uint)(buffer[offset + 3]));
|
||||
// }
|
||||
|
||||
// private static void WriteNetworkByteOrder(byte[] buffer, int offset, uint value)
|
||||
// {
|
||||
// buffer[offset + 0] = (byte)(value >> 24);
|
||||
// buffer[offset + 1] = (byte)(value >> 16);
|
||||
// buffer[offset + 2] = (byte)(value >> 8);
|
||||
// buffer[offset + 3] = (byte)(value >> 0);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
//}
|
||||
Reference in New Issue
Block a user