Wednesday, July 4, 2018

How to add SSL to Website...

The steps to add the SSL to a website are as follows.
1) We need to request for the CSR (Certificate Signing Request) from where our website is hosted.

What is a CSR?
 A CSR or Certificate Signing request is a block of encoded text that is given to a Certificate Authority when applying for an SSL Certificate. It is usually generated on the server where the certificate will be installed.

2) Once requested you need to provide the below mention details.
Common Name (Domain Name)
Organization Name
Organizational Unit  (Department)
City/Locality
State/County/Region 
Country  
Email address  

 
Once done they will provide you with the CSR

CSR Format
Example

-----BEGIN CERTIFICATE REQUEST-----
MLLByjCCATMCAQAwgYkxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
Q0uA0aVog3f5iJxCa3Hp5gxbJQ6zV6kJ0TYsuaaOhEko9sdpCoPOnRBm2i/XRD2D
8iNh8f8z0ShGsFqjDgFHyF3o+lUyj+UC6H1QE7bn
-----END CERTIFICATE REQUEST-----

Note*- When you are trying to generate the SSL Certificate copy all the CSR including the -----BEGIN CERTIFICATE REQUEST----- & -----END CERTIFICATE REQUEST-----

3) Now you can go and get the SSL for your website from SSL providers like


 and use the CSR to generate your SSL.

Restrict modifying the URL or Route in MVC

To restrict the user from modifying the URL. Write the following code in the Global.asax.cs file.

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
        public class NoDirectAccessAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (filterContext.HttpContext.Request.UrlReferrer == null ||
                            filterContext.HttpContext.Request.Url.Host != filterContext.HttpContext.Request.UrlReferrer.Host)
                {
                    filterContext.Result = new RedirectToRouteResult(new
                                   RouteValueDictionary(new { controller = "Error", action = "Index", area = "" }));
                }
            }
        }

Then add this Action Filter to Controller [MvcApplication.NoDirectAccess]

How to manage Global Level Exception's

We can handle the Global Level Exceptions by creating the custom exception filter in our application. To create custom exception filter do the following things.
1) Create a Class in the App_Start folder inherit the IExceptionFilter interface.
2) Override the OnException() method.
3)  Create the object of the System.Exception Class.

public class LogFilter : IExceptionFilter
    {
                public void OnException(ExceptionContext context)
        {
                    Exception ex = context.Exception;
            if (!(ex is HttpException)) //ignoring the file not found exception
            {              
             //Do your stuff.....!!!
            }
        }
    }

4) Register the new Custom Filter into the Filter.Config.cs

filters.Add(new LogFilter());

5) Register the filter in Global.asax.cs file.

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

Note*- For using this we must enable the custom exception in the Web.config file and use the [Handle Error] action filter on either on the controller or action method.

Handling Errors in MVC

We can handle the errors in the MVC application by just enabling the custom errors tab in the web.config file.
<customErrors mode="On" defaultRedirect="Error">
 This will only handle the Internal Server Error Exceptions like HTTP Status Code 500 and won't handle the HTTP Status Code Exceptions like 404,405 etc for that we need to do the following.
   <customErrors mode="On" defaultRedirect="Error">
      <error statusCode="404" redirect="Error/NotFound" />
    </customErrors>
Here Error is the Controller Name and NotFound is the name of the Action Method after that we need to set the [HandleError] action filter on either the Controller or the Action Method as per our requirement.

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...