ASP.NET Site Move HTTP to HTTPS, Refresh Google Indexing

When a site is moved from HTTP to HTTPS which is already indexed by Google, we must add a new property for the HTTPS URL in Goolge search console. When Google tries to index the site and it finds HTTP URL indexed for the same page, it will mark the HTTPS pages as canonical. We should use “301 Moved Permanently” to tell Google that HTTP URLs are moved to HTTPS and Google should prefer HTTPS over HTTP. Here is a sample code to be used in an ASP.NET web application for 301 moved status

Redirect HTTP URLs to HTTPS in Windows Hosting

After configuring SSL for a website, we need to make sure all existing HTTP URLs are automatically redirected to the HTTPS. For this we need to add a redirect rule in our web.config file. Open your web.config file and add following code snippet under system.webServer tag (copy it before the closing system.webServer tag) Save web.config file and upload it to the server tp replace existing one.

Configuration Options to Secure ASP.NET Application

If you have ASP.NET website on internet, you must make sure to implement following cofiguration steps to secure your website. Block libwww-perl attack in ASP.NET Application hosted in IIS – Follow this article to configure this. Some response headers reveal technical details about the server which must be removed. For example a sample response from an ASP.Net application may look like this In this response “Server”, “X-AspNet-Version”, “X-Powered-By” headers are revealing technical details about the server. We can remove these unnecessary IIS response headers as following Remove “X-Powered-By” Header – Open web.config and check for customHeaders tag. If this is not already there, then add it as child of “<httpProtocol>” and add “remove” entry for X-Powered-By as shown below <configuration> <system.webServer> <httpProtocol> <customHeaders> <remove name=”X-Powered-By” /> </customHeaders> </httpProtocol> </system.webServer> </configuration> You should also check the response from your Asp.Net application if this is using a shared hosting which may add additional server specific information to response headers. Add remove entry[…]

Quick Tip

Send Email from GoDaddy Asp.Net Application

In my previous article we looked at how to use Google account and SMTP server to programatically send mail to an email address. We can use the same code with minor changes for GoDaddy application which is using shared hosting. We will develop a “Contact us” page here using SMTP server from GoDaddy. The first step is to create a web service using System; using System.Configuration; using System.Net.Mail; using System.Web.Services; namespace GoDaddy.Email { /// <summary> /// Summary description for Mail /// </summary> [WebService(Namespace = “http://tempuri.org/”)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class Mail : System.Web.Services.WebService { [WebMethod] public string SendEmail(string email, string name, string body) { try { var toEmailAddress = ConfigurationManager.AppSettings[“ToEmailAddress”].ToString(); var smtpHost = ConfigurationManager.AppSettings[“SMTPHost”].ToString(); var smtpPort = ConfigurationManager.AppSettings[“SMTPPort”].ToString(); MailMessage mailMessage = new MailMessage(); mailMessage.To.Add(toEmailAddress); mailMessage.From = new MailAddress(email, name); mailMessage.Subject = “Contact[…]

Quick Tip

Send Email from Asp.Net Application using Google Account and SMTP

In order to send mail from the application we can use SMTP server from Google (which off course has certain limitations). We can use a Google mail account and send the mail using SMTP server from Google. This works very well for Asp.net applications which need to use a simple form such as “Contact Us”. An example of this is explained below. We will use AJAX to send the request to an ASP.Net web service which sends the mail from the server side. Create a web service as following using System; using System.Configuration; using System.Net.Mail; using System.Web.Services; namespace GoDaddy.Email { /// <summary> /// Summary description for Mail /// </summary> [WebService(Namespace = “http://tempuri.org/”)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class Mail : System.Web.Services.WebService { [WebMethod] public string SendEmail(string to, string subject, string body)[…]

Quick Tip

Different MasterPage for Mobile and Desktop in ASP.NET

Now a days more people have been accessing the web on mobile devices as compared to desktops and creating mobile versions of websites have become a necessity. In ASP.NET Web Forms application we can switch between different mastre pages basis which device user is using to access the site. This gives a lot of advantages such as changing layout of the pages, applying different style sheets and even load different content if the need be. To achieve this im ASP.NET Web Forms application simply define a base page as following public partial class BasePage : System.Web.UI.Page { protected void Page_PreInit(Object sender, EventArgs e) { if (Request.Browser.IsMobileDevice) { this.MasterPageFile = “~/Mobile.master”; } else { this.MasterPageFile = “~/Desktop.master”; } } } Now rest of the pages (such as example below) can simply inherit base page and all those pages will have different master pages for mobile and desktop. public partial class MyPage :[…]

Quick Tip

Bundling and Minification in ASP.NET Web Forms Application

ASP.NET has in-built for bundling of multiple resources such as js or css files into one file and then minifiy the files to reduce the number of calls made to the server and total data size downloaded from the server thus reducing the total download time and enhanding application’s performance.  In order to use this in web forms application which target version .NET 4.5 and higher, following steps are required 1. Go to NuGet Package Manager and install Microsoft.AspNet.Web.Optimization and Microsoft.AspNet.Web.Optimization.WebForms packages 2. Check web.config to make sure “webopt” tag prefix is added (once you install Microsoft.AspNet.Web.Optimization.WebForms) <pages> <namespaces> <add namespace=”System.Web.Optimization” /> </namespaces> <controls> <add assembly=”Microsoft.AspNet.Web.Optimization.WebForms” namespace=”Microsoft.AspNet.Web.Optimization.WebForms” tagPrefix=”webopt” /> </controls> </pages> 3. Add a class file as following and define the resources you want to bundle public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { Bundle cs = new Bundle(“~/bundles/cssv1”, new CssMinify()); cs.Include(“~/Resources/css/bootstrap.min.css”, “~/Resources/css/app.css”); bundles.Add(cs); bundles.Add(new ScriptBundle(“~/bundles/jsv1”).Include( “~/Resources/js/jquery.min.js”, “~/Resources/js/bootstrap.min.js”)); BundleTable.EnableOptimizations = true;[…]