DotNetTutorial

childlikenumberSecurity

Nov 5, 2013 (3 years and 9 months ago)

76 views

ASP.NET

The Clock Project

The ASP.NET Clock Project

The ASP.NET Clock Project is the topic of
Chapter 23
. By completing the clock
project, you will learn your way around
Visual Studio .NET.

After launching the development
environment for the first time, you will
explore its windows and practice using
controls that enable you to create, edit,
run, and debug an application.

Let us work through the clock project
together.

Creating an ASP.NET Project

Put Visual Studio .NET on your
Windows taskbar to start it up
quickly.

Create a new project with Visual
Studio .NET.

Understand what is meant by the
term Web Form.

The Clock Script

Create an ASP.NET page that tells
the time.

In HTML view, we write the following
code:

<h1>The Clock Strikes</h1>

The time is

<% = DateTime.Now.ToLongTimeString()
%>

Running an ASP.NET Project

Use the start button to run a project.

Debug a project containing errors.

Understand how actions you take in
Design View change the code in HTML
View.

Using the Toolbox

Open the toolbox and drag controls
onto the design surface.

Modify properties of controls on the
design surface.

Discover how the design surface uses
absolute positioning to place objects
on screen.

Illustrate this by creating an Update
button.

ASP.NET Top Secret Project

Part 6: Goals and Objectives

The Top Secret Project is the topic of
Part 6 of Advanced Web Design.

It enables you to look further under the
hood of ASP.NET.

You learn how to create a database with
Microsoft Access or SQL Server, populate
the database with user names and
passwords, and use ADO.NET to decide
who can access your site.

Top Secret Project

Chapter 24: Creating the Database

Chapter 24

gets you started by having you
use either Microsoft Access or SQL
Server to create the Top Secret database.

You make a Users table that will contain
the names and passwords of your users.

You populate the database with a few users
allowed to access your top secret stuff.


Top Secret Project

Chapter 25: Listing the Users

In
Chapter 25
, you write an ASP.NET script that
reads information from the Top Secret database
and displays it in a listbox on a Web page.

The design process you follow in this chapter will
teach you how to use a code
-
behind page to
separate your programming from the Web page.

You will call upon the assistance of the Code
Designer to help you create the code
-
behind page,
on which you will write a little program that uses a
data adapter to connect to a database.

After issuing an SQL query that fills a dataset
with the names of your Top Secret users, you will
read from the dataset and load into the listbox
the name of each user permitted access to the
Top Secret database.


Top Secret Project

Chapter 26: Adding New Users

At the moment, if you want to add a user
to the Top Secret database, you need to
use your database software (i.e., Microsoft
Access or SQL Server) to do so.

Suppose you are at a remote location, away
from your server, and the need arises to
add another user to the database. Wouldn’t
it be nice to have a way to add new users
remotely?

In
Chapter 26
, you will add to the
Users.aspx Web page an Add User button
that enables you to add new users remotely
over the Web.

Top Secret Project

Chapter 27: Updating Users

You need to update a database when
information changes in a dataset.

Suppose one of your Top Secret users gets
a new e
-
mail address, for example.

Changing that user’s e
-
mail address
requires you to connect to the database,
locate the user’s record, change the e
-
mail
address in the dataset, and update the
database.

In
Chapter 27
, you will learn how to make
these kinds of changes in a database.

Top Secret Project

Chapter 28: Deleting Users

In
Chapter 28
, you will create a Delete
button that will enable you to remove users
from the database who no longer should
have access to your top secret information.

When you write the code for the Delete
button, you will learn the dataset method
for deleting records from a database. This
will round out the database functions you
have learned in this part of this book.

By the time you complete this chapter, you
will have learned how to insert, update,
retrieve, and delete records from a
database.

Top Secret Project

Chapter 29: Logging Users In

Authentication is the process of determining whether a
user should be permitted or denied access to a resource
such as a Web site. On the Internet, Web sites that are
not publicly accessible require users to log in to gain
access.

In
Chapter 29
, you will create a login screen for the Top
Secret project. The login screen will contain three
controls: a text field into which the user will type a user
name, a text field into which the user will type a password,
and a login button.

When the user presses the login button, a script will
gather the name and password from the login form and
check to see whether the user should be permitted
access. If so, the script will set an authentication cookie
that will permit the user to access the Web site. If the
name and password are not recognized, on the other hand,
the user will be denied access to your top secret stuff.

Top Secret Project

Chapter 26: Adding New Users

At the moment, if you want to add a user
to the Top Secret database, you need to
use your database software (i.e., Microsoft
Access or SQL Server) to do so.

Suppose you are at a remote location, away
from your server, and the need arises to
add another user to the database. Wouldn’t
it be nice to have a way to add new users
remotely?

In
Chapter 26
, you will add to the
Users.aspx Web page an Add User button
that enables you to add new users remotely
over the Web.

Top Secret Project

Adding a Button

To add a button to an ASP.NET page,
follow these steps:

Drag a button from the WebForms
toolbox to the design surface.

Edit the button’s properties to give it
the text and ID you want it to have.

Double
-
click the button to make the
code generator create its handler.

Write the script to handle the button.

Top Secret Project

Add User Script

The add user script performs the
following tasks:

Get the data from the form.

See if the user already exists.

Add the new user to the data table.

Update the database.

Display a feedback message

Top Secret Project

Chapter 27: Updating Users

You need to update a database when
information changes in a dataset.

Suppose one of your Top Secret users gets
a new e
-
mail address, for example.

Changing that user’s e
-
mail address
requires you to connect to the database,
locate the user’s record, change the e
-
mail
address in the dataset, and update the
database.

In
Chapter 27
, you will learn how to make
these kinds of changes in a database.

Top Secret Project

Update User Script

The update user script performs the
following tasks:

Get the user name from the form.

Make sure the user name exists.

Update the fields in the data table.

Update the database.

Display a feedback message.

Top Secret Project

Chapter 28: Deleting Users

In
Chapter 28
, you will create a Delete
button that will enable you to remove users
from the database who no longer should
have access to your top secret information.

When you write the code for the Delete
button, you will learn the dataset method
for deleting records from a database. This
will round out the database functions you
have learned in this part of this book.

By the time you complete this chapter, you
will have learned how to insert, update,
retrieve, and delete records from a
database.

Top Secret Project

Delete User Script

The delete user script performs the
following tasks:

Get from the form the user name to be
deleted.

Make sure the user exists.

Delete the user from the data table.

Update the database.

Display a feedback message

Chapter 29: Logging Users In

Design the
Login.aspx

page, which
contains four labels, two text fields,
and one button .

Code and test the Login button.

Make the login button set the
authentication cookie.

Configure the authentication mode in
the Web.config file.

Setting the

Authentication Cookie

To set the authentication cookie,
make the code that handles the login
button issue the following command if
the username and password are valid:

System.Web.Security.FormsAuthentication
.SetAuthCookie(sUserName, false);

False causes the cookie not to get written
to the user’s hard drive.

Configuring the

Authentication Mode

Open the project’s Web.config file.

Make the <authentication> section
read as follows:

<authentication mode="Forms">


<forms loginUrl="login.aspx"></forms>

</authentication>

This causes unauthenticated users to
get the login.aspx page if they try to
access any other page in this app.

Identifying the User

When a user has logged on, you can
find out the user’s name via the
following code:

String sUserName = "";

if (HttpContext.Current.Request.IsAuthenticated)


sUserName = HttpContext.Current.User.Identity.Name;