Month: August 2013

Set CacheControlMaxAge to High Number

It’s a good practice to set the CacheControlMaxAge attribute in web.config to a high number.

<system.webServer>
	<staticContent>
		<!-- Set expire headers to 30 days for static content-->
		<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
	</staticContent>
</system.webServer>
Advertisements

Turn On HTTP Compression

It’s a good practice to enable HTTP compression for any uncompressed content. Add the following code snippet in web.config.

<system.webServer>
	<!-- GZip static file content. Overrides the server default which only compresses static files over 2700 bytes -->
	<httpCompression directory="%SystemDrive%\websites\_compressed" minFileSizeForComp="1024">
		<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
		<staticTypes>
			<add mimeType="text/*" enabled="true" />
			<add mimeType="message/*" enabled="true" />
			<add mimeType="application/javascript" enabled="true" />
			<add mimeType="application/json" enabled="true" />
			<add mimeType="*/*" enabled="false" />
		</staticTypes>
	</httpCompression>
</system.webServer>

Remove Unused View Engines

By default, ASP.NET MVC loads both the Razor and Web Forms View Engines. This can cause performance issues. Because ASP.NET will look for Web Forms views first, and switch to look for Razor views if Web Forms views cannot be found.

This can be resolved easily by adding the following 2 lines to the Application_Start method in Global.asax.cs.

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());

Implement Password Expiration Check in ASP.NET MVC

1. Create SkipPasswordExpirationCheckAttribute which represents to skip the Password Expiration Check

using System;

namespace Unfuddle.MvcWebApp.Web.Mvc
{
    [AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class SkipPasswordExpirationCheckAttribute : Attribute
    {
    }
}

2. Create PasswordExpirationCheckAttribute attribute which inherited from AuthorizeAttribute

using System;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

namespace Unfuddle.MvcWebApp.Web.Mvc
{
    [AttributeUsageAttribute(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class PasswordExpirationCheckAttribute : AuthorizeAttribute
    {
        private int _maxPasswordAgeInDay;

        public PasswordExpirationCheckAttribute(int maxPasswordAgeInDay = int.MinValue)
        {
            _maxPasswordAgeInDay = maxPasswordAgeInDay;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (!filterContext.ActionDescriptor.IsDefined(typeof(SkipPasswordExpirationCheckAttribute), inherit: true)
                && !filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(SkipPasswordExpirationCheckAttribute), inherit: true))
            {
                if (_maxPasswordAgeInDay != int.MinValue)
                {
                    IPrincipal user = filterContext.HttpContext.User;

                    if (user != null && user.Identity.IsAuthenticated)
                    {
                        MembershipUser membershipUser = Membership.GetUser(user.Identity.Name);

                        if (membershipUser != null)
                        {
                            TimeSpan timeSpan = DateTime.Today - membershipUser.LastPasswordChangedDate.Date;

                            if (timeSpan.Days >= _maxPasswordAgeInDay)
                            {
                                HttpContext httpContext = HttpContext.Current;
                                HttpContextBase httpContextBase = new HttpContextWrapper(httpContext);
                                RouteData routeData = new RouteData();
                                RequestContext requestContext = new RequestContext(httpContextBase, routeData);
                                UrlHelper urlHelper = new UrlHelper(requestContext);

                                filterContext.HttpContext.Response.Redirect(urlHelper.Action("ChangePassword", "Account", new { reason = "passwordExpired" }));
                            }
                        }
                    }
                }
            }

            base.OnAuthorization(filterContext);
        }
    }
}

3. Add a setting MaxPasswordAgeInDay in ~\Properties\Settings.settings

MaxPasswordAgeInDay in Settings
MaxPasswordAgeInDay in Settings

4. Register PasswordExpirationCheckAttribute in ~\App_Start\FilterConfig.cs

filters.Add(new PasswordExpirationCheckAttribute(Settings.Default.MaxPasswordAgeInDay));

5. Register SkipPasswordExpirationCheckAttribute in LogOn and ChangePassword action methods of the AccountController

[AllowAnonymous]
[SkipPasswordExpirationCheck]
public ActionResult LogOn()
{
	......
}

[AllowAnonymous]
[SkipPasswordExpirationCheck]
[HttpPost]
public ActionResult LogOn(LogOnViewModel model, string returnUrl)
{
	......
}

[AllowAnonymous]
[SkipPasswordExpirationCheck]
public ActionResult ChangePassword(string reason = null)
{
	......
}

[AllowAnonymous]
[SkipPasswordExpirationCheck]
[HttpPost]
public ActionResult ChangePassword(ChangePasswordViewModel model)
{
	......
}

References

Restore SQL Server Database

1. In SQL Server Management Studio, connect to the server as sa

2. Expand Databases

Microsoft SQL Server Management Studio
Microsoft SQL Server Management Studio

3. Right-click the database, point to Tasks, point to Restore, and then click Database…

Restore Database - General Page
Restore Database – General Page

4. In Restore Database dialog, verify the destination database name

5. On the General page, specify the source and location of backup sets to restore

Restore Database - Options Page
Restore Database – Options Page

6. On the Options page, select Overwrite the existing database (WITH REPLACE)

7. Click OK to start restore

Restore Completed
Restore Completed

References

Backup SQL Server Database

1. In SQL Server Management Studio, connect to the server as sa

2. Expand Databases

Microsoft SQL Server Management Studio
Microsoft SQL Server Management Studio

3. Right-click the database, point to Tasks, and then click Back Up…

Back Up Database
Back Up Database

4. In Back Up Database dialog, verify the database name

5. In the Backup type dropdown, select Full

6. Specify the backup destination

7. Click OK to start backup

Backup Completed
Backup Completed

References