Wednesday, December 26, 2018

Howto convert a PFX to a seperate .key/.crt file

openssl pkcs12 -in [yourfile.pfx] -nocerts -out [keyfile-encrypted.key]
What this command does is extract the private key from the .pfx file. Once entered you need to type in the importpassword of the .pfx file.  This is the password that you used to protect your keypair when you created your .pfx file.  If you cannot remember it anymore you can just throw your .pfx file away, cause you won’t be able to import it again, anywhere!.  Once you entered the import password OpenSSL requests you to type in another password, twice!. This new password will protect your .key file.
Now let’s extract the certificate:
openssl pkcs12 -in [yourfile.pfx] -clcerts -nokeys -out [certificate.crt]
Just press enter and your certificate appears.
Now as I mentioned in the intro of this article you sometimes need to have an unencrypted .key file to import on some devices.  I probably don’t need to mention that you should be carefully. If you store your unencrypted keypair somewhere on an unsafe location anyone can have a go with it and impersonate for instance a website or a person of your company.  So always be extra careful when it comes to private keys! Just throw the unencrypted keyfile away when you’re done with it, saving just the encrypted one.
The command:
openssl rsa -in [keyfile-encrypted.key] -out [keyfile-decrypted.key]
Again you need to enter an import password. This time you need to enter the new password that you created in step 1.  After that you’re done. You decrypted your private key. In the folder you ran OpenSSL from you’ll find the certifcate (.crt) and the two private keys (encrypted and unencrypted).

Ref: https://www.markbrilman.nl/2011/08/howto-convert-a-pfx-to-a-seperate-key-crt-file/
Ref: https://aws.amazon.com/blogs/security/how-to-import-pfx-formatted-certificates-into-aws-certificate-manager-using-openssl/#:~:text=To%20import%20the%20certificates,Select%20Import%20a%20certificate.

Sunday, August 26, 2018

Self Contained ASP.NET Core Deployments

dotnet publish ProjectName.csproj --self-contained:true --runtime:win10-x64 /nologo /p:PublishProfile=Release  /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /maxcpucount:1 /p:platform="Any CPU" /p:configuration="Release" /p:DesktopBuildPackageLocation="$\project.zip"

--runtimeIdentifier: it is necessary. Also, you need to specify the supported runtime identifier in your poject file.
(https://docs.microsoft.com/en-us/dotnet/core/rid-catalog)

Commands
https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-publish?tabs=netcore21

Wednesday, June 27, 2018

How to take dumps for an Azure Web App from Debug Cosole - Kudu

Create the folder where you would like to take dumps and then run following command.

Get the process id from the process explorer.

d:\devtools\sysinternals\procdump -accepteula -ma {Process ID}

Monday, May 28, 2018

Git Commands

To revert local check in. 
git reset Head~1

To view the changes recently reset, 
git reflog show HEAD
or 
git reflog.

This will show you your recent resets and other history.
You can restore it again using following command.
git reset --hard <commit hash>

above command will destroy any local modifiction so better stash them.


To merge your development branch into master.  

1. Check out the branch. 

git checkout development

2.Merge your changes from master into development. (This step is necessary so that you can resolve any merge conflict locally in your development branch. )

git merge development

3.  Check out the master branch now. 

git checkout master

4. Merge the development branch into master. (Make sure to use --no-ff to persist your history.)

git merge --no-ff development. 

5. Push your changes to the master. 




Thursday, May 10, 2018

Getting all Users from AAD using Power Shell and Delete

Connect-MsolService
Get-MsolUser
#get the user principle name.
Connect-AzureAD
Remove-AzureADUser -ObjectId "xxxx.onmicrosoft.com#EXT#@yyyy.onmicrosoft.com"

UPN of the external accounts is as shown above. 

Thursday, May 3, 2018

Storing secrets using SecretManager for development in ASP.NET Core

The purpose of using this is to keep the secured values outside of the code to avoid check in by mistake.

- Modify the .csproj file and add following "Microsoft.Extensions.SecretManager.Tools"

- Save and close.
- Right click the project and select "Manage User Secrets"
- this will open JS. store your secret values in there.  This json file is stored away from the project on local machine.
- Add following into your Startup.cs file.

if (_hostingEnvironment.IsDevelopment())
            {
                builder.AddUserSecrets<Startup>();
            }

- read the secret using Configuration. like if the secret name is "MySecret", read it like Configuration["MySecret"]
-This should only be used in development.
- For production, code will read the value from appsettings.json. 

Routing Trace to Console

ConsoleTraceListener listener = new ConsoleTraceListener();
Trace.Listeners.Add(listener);

Trace.WriteLine("Trace Logs");

Trace.Listeners.Remove(listener);

Trace.Close();