Posts

ASPNet Core checks whether current connection is secured

using APS.net Core to mark all website pages working with https protocol we will do this using IAuthorizationFilter. and here is an example how we can do it. /// <summary>     /// Represents a filter attribute that checks whether current connection is secured and properly redirect if necessary     /// </summary>     public class HttpsRequirementAttribute : TypeFilterAttribute     {         #region Fields         private readonly SslRequirement _sslRequirement;         #endregion         #region Ctor         /// <summary>         /// Create instance of the filter attribute         /// </summary>         /// <param name="sslRequirement">Whether the page should be secured</param>         public HttpsRequirementAttribu...

Create and register configuration parameters into service collection asp.net core

supposed you have a json file with a specific parameter and you want to fill a class with the specified properties. and then register this object to the asp.net core service  collection here is an example how to do it. /// Create, bind and register as service the specified configuration parameters         /// </summary>         /// <typeparam name="TConfig">Configuration parameters</typeparam>         /// <param name="services">Collection of service descriptors</param>         /// <param name="configuration">Set of key/value application configuration properties</param>         /// <returns>Instance of configuration parameters</returns>         public static TConfig ConfigureStartupConfig<TConfig>(this IServiceCollection services, IConfiguration configuration) where TConfig : class, new() ...

How to run batch file using Concole application

To run a .exe file using console application you need to do the following. Process proc = new Process(); proc.StartInfo.WorkingDirectory = _filePath; proc.StartInfo.FileName = _fileName; proc.StartInfo.CreateNoWindow = false; proc.Start(); proc.WaitForExit();

Search and replace regular expressions group VS2017

Sometimes you write some codes and then you find your self want to change a suffix or prefix in your codes or classes names or namespaces I will show with an example how to do it.  let us supposed we need to change the following "Manager" sub-string into the class name public interface IParticipantImageManager in visual studio click on ctrl + shift + H use any website to test your regular expression, personally I use  regex101  to write the right expression and the replace it. to find the string IParticipantImage(\w+) and to replace the sting IParticipantImage$1

Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0

Visual Studio 2018   stuck after lunching specific solution. after spending sometime on the internet, I found the problem and the fix. I used Community Edition for visual studio 2018. 1- Close all running instances of Visual Studio 2017 2- Launch the Visual Studio 2017 Developer Command Prompt as Admin 3- Type the following commands (replace Community with your edition, either Enterprise or Professional , or adjust the path accordingly): gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Framework.dll" gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.dll" gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Engine.dll" gacutil /i "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Build.Conversion.Core.dll" gacutil /i "C...

How to Stop debugging method or class in c#

Sometimes you need to step through code, So you use debugging in Visual Studio. But some other times you visit the same method over and over again for 100 times if and only if the code is tested. then you can do decorate your method with this attribute  [DebuggerStepThrough()] and your debugger will never hit this method again

cannot access a disposed object. Object name: 'WorkspaceContext' TFS VS2017

problem: I have a simple project hosted on our company TFS. our security policy required the development team change there passwords every 1 month. after changing my password, I recived this error while check In TFS Cannot access a disposed object. Object name: 'WorkspaceContext' solution: You need to visit this location %LocalAppData%\Microsoft\Team Foundation\7\Cache inside this location delete the content of this folder and try to connect again to visual studio.

Authenticate ASPNet Core from Azure

First you need to create a section in appsettings.json file   "AzureAd": {     "Instance": "https://login.microsoftonline.com/",     "Domain": "Your Domain",     "TenantId": "Your Tenant Id",     "ClientId": "Your Client Id",     "CallbackPath": "/signin-oidc"   } then I usually use extension method for the service collection public static void AddApplicationAuthentication(this IServiceCollection services)         {             IConfiguration Configuration = services.BuildServiceProvider().GetRequiredService<IConfiguration>();             services.Configure<CookiePolicyOptions>(options =>             {                 // This lambda determines whether user consent for non-essential cookies is needed for a given request.         ...

Get months names using CultureInfo C#

To get the month names using a given Culture Info in a simple select list var monthOfBirthValues = new List (); monthOfBirthValues.Add(new SelectListItem  {      Text = CultureInfo.GetCultureInfo("ar-KW").DateTimeFormat.GetMonthName(i),     Value = i.ToString(CultureInfo.InvariantCulture)  });