Posts

Self Hosted Microservice using OWIN, WebApi to authenticate users

Image
Hi Guys, This time I created Self Hosted Microservice using OWIN, WebApi to authenticate users, this service will store the authentication token on a file and subsequent requests are compared against the stored token. Hope you like it. 1) Create a console application as following 2) Install following packages using Nuget  Microsoft.AspNet.WebApi.OwinSelfHost Microsoft.Owin Microsoft.AspNet.Cors 3) Startup.cs  using Microsoft.Owin; using Microsoft.Owin.Security.OAuth; using Owin; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using System.Web.Http; namespace SelfHosted.MicroService {     public class Startup     {         // This code configures Web API. The Startup class is specified as a type         // parameter in the WebApp.Start method.       ...

Consuming a secured JWT webapi using ionic, angular 2

Image
I had a requirement to create an app which consumes a secured JWT webapi using ionic and angular 2. This is how I did it. Hope you like. Reference :  http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-and-identity-2/ 1) Open Startup.cs and paste the following code using Microsoft.Owin; using Microsoft.Owin.Security; using Microsoft.Owin.Security.DataHandler.Encoder; using Microsoft.Owin.Security.Jwt; using Microsoft.Owin.Security.OAuth; using Owin; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Web; using System.Web.Http; [assembly: OwinStartup(typeof(SecuredWebAPI.Startup))] namespace SecuredWebAPI {     public class Startup     {         public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }         public static string PublicClientId { get; private set; }       ...

Secured Token Based WebAPI with custom authentication

Image
Hi Guys, I had a requirement to create Secured  Token Based WebAPI which can be consumed using mobile applications. And it had to check a custom authentication stored in database. Upon successful login an auth token would be returned subsequent requests must pass this auth token to get data. Reference : http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/ 1) Create a WebAPI Project as following 2) Modify the Startup.cs as following using Microsoft.Owin; using Microsoft.Owin.Security.OAuth; using Owin; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; [assembly: OwinStartup(typeof(SecuredWebAPI.Startup))] namespace SecuredWebAPI {     public class Startup     {         public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }         public sta...

Uploading images using WebApi and JSON

Image
Hi All, I recently had a requirement to create a Web API to work with a mobile phone. I was required to create a method to upload images. So I did it the following way hope you like it. 1) Class Definitions  public class AuthData     {         public string auth_token { get; set; }     }        public class ImageSetData : AuthData     {         public string Name { get; set; }         public List<Image> Images { get; set; }     }     public class Image     {         public string FileName { get; set; }         public string Extension { get; set; }         public byte[] ImageData { get; set; }     } 2) WebApi Method public ReturnMsg SaveFile([FromBody] ImageSetData imageData)         {           ...

WCF Service and TLS 1.2

Hi All, I worked on a WCF Service (.NET 3.5) which consumed an external web service and was working fine. Until recently it started giving the following error. An error occurred while making the HTTP request to {url}.This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case.This could also be caused by a mismatch of the security binding between the client and the server. This made a big issue as the WCF was already being consumed by number of other systems. Later we got to know the external web service was upgraded to use TLS 1.2 and the .NET 3.5 doesn't support TLS 1.2 (.NET 4.5 or greater supports TLS 1.2). So I had to change the version of the .NET version to 4.5 and add the following code to enforce it to use TLS 1.2. And everything started to work the way they used to. 1) .NET 4.6 and above. You don’t need to do any additional work to support TLS 1.2, it’s supported by default. 2) .NET 4.5. TLS 1.2 is sup...

Bilingual Dynamic Survey

Image
Hi Guys, I created a bilingual Dynamic Survey application using ASP.NET. Hope you like it.

Request String Encryption in MVC 5

Image
Hi Guys, This time I had a requirement to Encrypt the request string parameters before passing them to the Controller's method. After referring this  solution , I made this solution I hope you like it. 1) SecurityHelper  public class SecurityHelper     {         public static string Encrypt(string plainText)         {             string key = "jdsg432387#";             byte[] EncryptKey = { };             byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };             EncryptKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));             DESCryptoServiceProvider des = new DESCryptoServiceProvider();             byte[] inputByte = Encoding.UTF8.GetBytes(plainText);             MemoryStream mStream ...