Sunday, February 26, 2017

Uploading images using WebApi and JSON

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)
        {
            ReturnMsg objReturnMsg = new ReturnMsg();

            try
            {
                if (APIHelper.AuthenticateToken(imageData.auth_token))
                {

                    foreach (var image in imageData.Images)
                    {
                        File.WriteAllBytes(string.Format("{0}\\{1}.{2}", HttpContext.Current.Server.MapPath("~/App_Data") , image.FileName, image.Extension), image.ImageData);
                    }  
                  




                }
                else
                {
                    objReturnMsg.success = false;
                    objReturnMsg.msg = "Invalid Token. Please Login to continue";
                }
            }
            catch (Exception ex)
            {

                objReturnMsg.success = false;
                objReturnMsg.msg = "Internal Server Error. Contact Admin.";
                APIHelper.Log(ex.Message, ex.StackTrace);
            }


            return objReturnMsg;
        }

3) Post Man Screen shot and Image




Wednesday, January 25, 2017

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 supported, but it’s not a default protocol. You need to opt-in to use it. The following code will make TLS 1.2 default, make sure to execute it before making a connection to secured resource:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

3) .NET 4.0. TLS 1.2 is not supported, but if you have .NET 4.5 (or above) installed on the system then you still can opt in for TLS 1.2 even if your application framework doesn’t support it. The only problem is that SecurityProtocolType in .NET 4.0 doesn’t have an entry for TLS1.2, so we’d have to use a numerical representation of this enum value:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

4) .NET 3.5 or below. TLS 1.2 is not supported (*) and there is no workaround. Upgrade your application to more recent version of the framework.


Sources : http://blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support/