If you use the above method, you need to check this validation in each and every pages of your web application. To avoid this, create a Base Page class which is inheritated from System.Web.UI.Page. All your pages(except login page) in your web application should inherit from this base page. Therefore you can write the above code to validate the user login in the page load event of the base page class. This code will be executed for all pages whenever the user access a page without login.
Alternatively, you can implement ASP.NET Forms Authentication and it will automatically redirect to login page if the user access any of the page without log-in.
To know more about forms authentication, visit the following links