Monday, December 24, 2018

Retrieve MimeType by its extension

In file IO, we need to set content/type while downloading it and this should be dynamically set by file's extension. Here is code we can use to do the same

Create static class to store all available mime-type, here am showing few only, we can add more.


public static class MimeTypesHelper
        {
            public const string
            Doc = "application/msword",
            Docx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            Xls = "application/vnd.ms-excel",
            Xlt = "application/vnd.ms-excel",
            Xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            Ppt = "application/vnd.ms-powerpoint",
            Pptx = "application/vnd.openxmlformats-officedocument.presentationml.presentation",
            Pdf = "application/pdf",
            Zip = "application/zip";


            //get matched mime type by extension, so property name should be equals to file extension
            public static string GetMimeTypeByExtension(string extension)
            {
                //remove period in extension
                extension = extension?.Replace(".", "");
                //match mimetype by property
                string matchedMimeType = typeof(MimeTypes).GetField(extension, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)?.GetValue(null)?.ToString();

                //if there is no match default to octet stream
                if (string.IsNullOrEmpty(matchedMimeType))
                {
                    matchedMimeType = "application/octet-stream";
                }

                return matchedMimeType;
            }
        }


Call this method to retrieve your file mimetype

 Response.ContentType = MimeTypesHelper.GetMimeTypeByExtension(".pdf");   //this will return "application/pdf"



Tuesday, August 7, 2018

SQL - Split different column names display with separator

In some situation, we need to show separator for firstname and middlename. This can be comma if there some values.

Lets take sample of employees data as below


With EmployeeCTE AS (
    SELECT * FROM (VALUES (1, 'Balaji', NULL),
    (2, 'Siva', NULL),
    (3,'Balaji', 'Prasad'),
    (4,NULL,'Raj'),
    (5,NULL,NULL),
    (6,'Karthi','m')) as E(Sno,FirstName,MiddleName) )


Now query this to get comma separate names out of this
      
SELECT    
p.sno,p.firstname,p.middlename,
IsNull(p.FirstName, '')
+ CASE
 WHEN p.MiddleName = ''
 THEN ''
 WHEN (IsNull(p.FirstName, '') <> '' and IsNull(p.MiddleName, '') <> '')
 THEN IsNull(', ' + p.MiddleName, '')
 ELSE IsNull(p.MiddleName, '') END As Name
FROM  EmployeeCTE AS p


Now the output will come as comma separated if there values present in firstname or lastname

sno
firstname
middlename
Name
1
Balaji
NULL
Balaji
2
Siva
NULL
Siva
3
Balaji
Prasad
Balaji, Prasad
4
NULL
Raj
Raj
5
NULL
NULL

6
Karthi
m
Karthi, m

Wednesday, June 13, 2018

OTP Mechanism in Asp.Net core

OTP mechanism can be done using different algorithms like TOTP or HOTP. To use it we have inbuilt plugins available,

I have used Otp.Net and TOTP algorithm for this purpose, we can refer it from https://github.com/kspearrin/Otp.NET

Step 1: Refer library from Nuget "Otp.Net" https://www.nuget.org/packages/Otp.NET

Step 2: Create TOTP Object


var emailToSend ="balajisrmv@gmail.com";
var secretKey = Encoding.ASCII.GetBytes(emailToSend);
var TotpObj = new Totp(secretKey, step: 60); //set step for 60 secs for OTP expiration
var otpString = TotpObj.ComputeTotp();

//Send to email, you can customize this to however needed.
emailService.SendEmail(toAddress: emailToSend, subject: "OTP Subject", body: "Your otp code is: " + otpString);


Step 3: Send this otpString to any channel like Email or SMS as your covenient

Step 4: Create seperate action method to validate input OTP code from user

public IActionResult OnPostVerifyAuthCodeAsync(string OtpCode)
        {
   var emailToSend ="balajisrmv@gmail.com";
   var secretKey = Encoding.ASCII.GetBytes(emailToSend);
   var TotpObj = new Totp(secretKey, step: 60); //set step for 60 secs for OTP expiration            
bool otpValid = TotpObj.VerifyTotp(OtpCode, out long timeStepMatched, new VerificationWindow(2, 2));

            if (otpValid)
            {
               
//OTP is valid proceed your business logic            

            }
            else
            {
               //OTP is invalid throw error
     }

            return Page();

        }

Monday, May 21, 2018

Angular 4 - How to detect device in browser

Its very simple and straight forward to use plugin for Angular 4 typescript as below

Use plugin https://www.npmjs.com/package/ngx-device-detector which can be added to package.json to install as reference

Include in your app.module as below

import { DeviceDetectorModule } from 'ngx-device-detector';


imports: [
      DeviceDetectorModule.forRoot()
    ],

Include in your component or service as below


import { DeviceDetectorService } from 'ngx-device-detector';

constructor(private deviceService: DeviceDetectorService) {   }

To get device info use as below in component

this.deviceService.getDeviceInfo();  //This will get all device useragent info 
this.deviceService.isTablet(); //This will get device is tablet or not
this.deviceService.isDesktop(); //This will get device is desktop or not
this.deviceService.isMobile(); //This will get device is mobile or not



Wednesday, May 2, 2018

VS - Asp.Net core application not worked and run only on fiddler

I have faced some specific issue, where .Net core project was not running on my local machine visual studio and ruins only when Telerik Fiddler ruins. It always goes to 500 error page (only for me). I have tried changing all browser proxy settings, but those not fixed my

I was searching for my Outlook automatic replies not opened and calendar schedules not shown issue, for that got fix from Microsoft as https://support.microsoft.com/en-in/help/2847833/proxy-server-causing-issues-in-outlook-with-free-busy-oof-and-mailtips, surprisingly it fixed my visual studio .net core project issue. Try below steps to reset proxy


Friday, February 16, 2018

Outlook 2016 integration to Skype for business Lync 2016

I got issue on outlook 2016 where Skype for business (Lync 2016) status for contacts not showing.

I have followed below steps and worked

1. Open RegEdit by typing in run command (WinKey + R)
2. Go to Path HKEY_CURRENT_USER\SOFTWARE\IM Providers
3. Update DefaultIMApp value as "Lync" as in screenshot below
4. Restart outlook



Ref: https://blogs.msdn.microsoft.com/rathomas/2012/12/03/outlook-2013-users-are-unable-to-see-the-presence-info-in-outlook/