How to use C #. Net to implement form-based authentication in ASP. NET? (1)

Source: Internet
Author: User

Http://support.microsoft.com/default.aspx? SCID = KB; en-US; 301240

This articleArticleThe following namespace is referenced in the Microsoft. NET Class Library:
System. Data. sqlclient
System. Web. Security
-------------------------------
Task:
Abstract:
1. Requirements
2. Use Visual C #. Net to create an ASP. NET applicationProgram
3. Configure security settings in the web. config file
4. Create a database table sample to store user data
5. Create a logon. ASPX page
6. Write event processingCodeTo verify user identity
7. Create a default. aspx page
8. Additional prompts

Summary
This article demonstrates how to implement form-based verification by storing user information in the database.
(1) Requirements
The following tools are required for implementation:
1. Microsoft Visual Studio. NET
2. Microsoft Internet Information Services (IIS) version 5.0 or update
3. Microsoft SQL Server
(2) Use C #. Net to create ASP. NET Applications
1. Open Visual Studio. NET
2. Create a new ASP. NET web application and specify the name and path.
(3) Configure security settings in the web. config file
This section demonstrates how to configure ASP. NET applications by adding and modifying <authentication> and <authorization> nodes to implement form-based verification.
1. Open the Web. config file in the solution window.
2. Change the Authentication Mode to forms (note: the default mode is windows)
3. insert the <forms> tag and enter the appropriate attributes. (please refer to the msdn document or Quickstart document listed at the end of the article to view these attributes.) first copy the following code and then paste it in the <authentication> section:

<Authentication mode = "forms">
<Form name = ". aspxformsdemo" loginurl = "Logon. aspx" Protection = "all" Path = "/" timeout = "30"/>
</Authentication>
(Note: If loginurl is not specified, the default value is default. aspx)

4. Add the following nodes to reject anonymous access:
<Authentication>
<Deny users = "? "/>
<Allow users = "*"/>
</Authentication>

(4) Create a database table sample to store user data
This section demonstrates how to create a sample database to store user names, passwords, and user roles. if you want to implement role-based security, it is necessary to add a field for storing user roles in the database.
1. Open notepad.
2. Copy the following script to notepad and save it:

If exists (select * From sysobjects where id =
Object_id (n' [DBO]. [users] ') and objectproperty (ID, n'isusertable') = 1)
Drop table [DBO]. [users]
Go
Create Table [DBO]. [users] (
[Uname] [varchar] (15) not null,
[PWD] [varchar] (25) not null,
[Userrole] [varchar] (25) not null,
) On [primary]
Go
Alter table [DBO]. [users] With nocheck add
Constraint [pk_users] primary key nonclustered
(
[Uname]
) On [primary]
Go

insert into users values ('user1', 'user1', 'manager')
insert into users values ('user2', 'user2', 'admin ')
insert into users values ('user3', 'user3', 'user')
go
3. open Microsoft SQL Server, open the query analyzer, select the pubs database in the Database List, and paste the above script to run it. In this case, an example User table will be created in the pubs database and will be used in this sample program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.