Skip to content

Instantly share code, notes, and snippets.

View mirsaeedi's full-sized avatar

Ehsan Mirsaeedi mirsaeedi

  • Microsoft
  • Vancouver, Canada
  • 10:58 (UTC -07:00)
  • X @mirsaeedi
View GitHub Profile
@mirsaeedi
mirsaeedi / Startup.cs
Created January 19, 2021 01:20
ASP.NET Core Suppress Default Validation Message
services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
@mirsaeedi
mirsaeedi / GitHubResilientDelegatingHandler.cs
Last active June 27, 2018 02:51
Resilient Octokit Client
using Octokit;
using Polly;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
@mirsaeedi
mirsaeedi / .cs
Created November 21, 2017 21:38
How to unlock BitLocker drives in Windows
using System.Management;
namespace Windows.Security.FileSystem
{
public class BitLocker
{
private readonly string _computerIp;
public BitLocker(string computerIp)
{
@mirsaeedi
mirsaeedi / .cs
Created November 21, 2017 21:31
Create ProcessStartInfo in Windows with custom user Context
public ProcessStartInfo GetProcessStartInfo(string domain,string username, string password, string path,string args)
{
SecureString securePwd = new SecureString();
foreach (char ch in _password)
securePwd.AppendChar(ch);
ProcessStartInfo processInfo = new ProcessStartInfo(path);
processInfo.UserName = username;
@mirsaeedi
mirsaeedi / AspnetWebApiUploadFile.cs
Created October 6, 2016 08:37
Helps you to save uploaded files to disk in ASP.Net Web API. The SaveFile method works with Request object directly.
public async Task<IHttpActionResult> SaveFile(string diskFolderPath)
{
var path = Path.GetTempPath();
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
}
MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(path);
@mirsaeedi
mirsaeedi / AspNetWebApiGetIP.cs
Created October 6, 2016 08:29
Helps you to catch the IP of the HTTP request
if (Request.Properties.ContainsKey("MS_HttpContext"))
{
var ctx = Request.Properties["MS_HttpContext"] as HttpContextWrapper;
if (ctx != null)
{
var ip = ctx.Request.UserHostAddress;
return ip;
}
}
@mirsaeedi
mirsaeedi / PersianCalendarUtility.cs
Created October 6, 2016 08:18
Helps you in conversion between Shami (Solar) and Gregorian dates.
public class PersianCalendarUtility
{
public static string ConvertToPersianDate(DateTime? dateTime)
{
return ConvertToPersianDate(dateTime, false);
}
public static string ConvertToPersianDate(DateTime? dateTime, bool showTime = false)
{
if(!dateTime.HasValue)
@mirsaeedi
mirsaeedi / IranianNationalCodeAnalyzer.cs
Created October 6, 2016 08:13
Helps you to validate Iranian National Identification Code.
public static bool IsValid(string nationalCode)
{
if (nationalCode.Trim().Length != 10)
return false;
try
{
var controlBit = int.Parse(nationalCode[9].ToString());
var sum = 0;
@mirsaeedi
mirsaeedi / GetEnumDisplayName.cs
Created October 6, 2016 08:11
An extension method which helps you to get the value of DisplayAttribute of an enum member.
public static string GetEnumDisplayName(this Enum value,string unknownValue="Unknown")
{
if(value == null)
{
return unknownValue;
}
var type = value.GetType();
if (!type.IsEnum) throw new ArgumentException(String.Format("Type '{0}' is not Enum", type));
var members = type.GetMember(value.ToString());
@mirsaeedi
mirsaeedi / IRShabaCodeAnalyzer.cs
Created October 6, 2016 08:07
IRShabaCodeAnalyzer helps you to validate Iranian inter bank identification code which name is Shaba Code
public static bool IsValid(string shabaCode)
{
if (string.IsNullOrWhiteSpace(shabaCode))
return false;
shabaCode = shabaCode.Trim();
if (!shabaCode.ToLower().StartsWith("ir"))
return false;