ASP.NET Web Pages Adding Security

## Web Building

HOME Web Empty Site Web Database

## PHP Demo

PHP Demo PHP Website PHP Styles PHP Database PHP Navigation PHP Ajax

## ASP Demo

ASP Demo ASP Website ASP Styles ASP Database ASP Login ASP Security ASP Navigation ASP Ajax

## .NET Web Pages

WebPages Demo WebPages Website WebPages Styles WebPages Database WebPages Login WebPages Security WebPages Navigation WebPages Ajax

## .NET Web Forms

WebForms Demo WebForms Website WebForms Styles WebForms Database WebForms Navigation WebForms Ajax

## Web Building

Web Design Web Quality Web WAI Web Validation Web SEO Web TCP/IP

## Web Hosting

Host Intro Host Providers Host Domains Host Capacity Host Email Host Technologies Host Databases Host Types Host E-commerce

# ASP.NET Web Pages - Adding Security

« Previous
Next Chapter »

Learn ASP.NET Web Pages by building a web site from scratch.

Part V: Adding Security.

## What We Will Do

In this chapter we will:

- Add access restriction security to a web page

## Membership, Login, and Security

In the previous chapter we added a registration page and a login page to the web site.

When a membership is registered, the user can login, with his email address and password.

However, user membership and login functions do not automatically restrict (or open) any access to the web site.

Security (access restrictions) has to be added to each of the web pages.

## Adding Security to Pages

Access restrictions (security) can easily be added to any web page.

Just put the following code inside the page:

## Security Code:

if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("Login.cshtml");
}

The code above executes an if test, asking if the user is logged in. If not, the user is redirected to the login page.

## Preventing Access to the Database

In our example, the page Customers.cshtml, lists customers from the "Northwind" database.

Add the security code at the beginning of the page:

## Customers.cshtml

@{
if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("Login");
}
Layout = "Layout.cshtml";
var db = Database.Open("Northwind");
var query = db.Query("SELECT CompanyName,City,Country FROM Customers");
}
<html>
<body>
<h1>Customers</h1>
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach(var row in query)
{
<tr>
<td>@row.CompanyName</td>
<td>@row.City</td>
<td>@row.Country</td>
</tr>
}
</table>
</body>
</html>

The Customers page above, is a copy of the page from the previous chapter about databases. The security code (added at the beginning) is marked red.

## Congratulations

You have added security to your web site, using the WebSecurity object.

For a full reference to the WebSecurity object, please visit our WebSecurity Object Reference.

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE