如何通过SQL Server2000使用Forms 身份验证
摘要 ASP.NET Forms 身份验证允许用户将凭据(用户名和密码)输入到 Web Form 来标识其身份。在收到这些凭据时,Web 应用程序可以根据数据源来检查这些凭据,从而对用户进行身份验证。 本模块描述如何使用密码哈希安全地将用户凭据存储在 SQL Server 中,以及
摘要
ASP.NET Forms 身份验证允许用户将凭据(用户名和密码)输入到 Web Form 来标识其身份。在收到这些凭据时,Web 应用程序可以根据数据源来检查这些凭据,从而对用户进行身份验证。
本模块描述如何使用密码哈希安全地将用户凭据存储在 SQL Server 中,以及如何根据包含在 SQL Server 中的帐户数据库对用户进行身份验证。
预备知识
安全地存储用户凭据包含两个关键概念:
• 存储密码摘要。出于安全性考虑,请不要将密码明文存储在数据库中。本模块描述如何创建和存储用户密码的单向哈希而非密码本身。如果要存储加密的用户密码,建议选择这种方法,,因为它避免了与加密技术相关的密钥管理问题。
为增加安全性并减轻与字典攻击相关的威胁,本模块中描述的方法在创建密码哈希前,将 salt(以加密方式生成的随机数)与密码结合起来。
• 重要事项 不将密码存储在数据库中的一个缺点是,一旦用户忘记密码,则无法恢复。因而,应用程序应使用密码提示,并将它们与密码摘要一起存储在数据库中。
• 验证用户输入。当将用户输入传递给 SQL 命令时,例如比较语句或模式匹配语句中的字符串,应非常小心地验证此输入,以确保最终的命令不包含语法错误,并且还要确保黑客不会使您的应用程序运行任意 SQL 命令。在登录过程中验证提供的用户名特别重要,因为应用程序的安全模型完全取决于是否能够正确而安全地对用户进行身份验证。
创建一个有登录页的 Web 应用程序
此过程创建一个简单的 Visual C# Web 应用程序,它包含一个用户可以输入用户名和密码的登录页。
要创建一个有登录页的 Web 应用程序,请执行下列步骤:
• 启动 Visual Studio .NET 并创建一个新的名为 FormsAuthSQL 的 Visual C# ASP.NET Web 应用程序。
• 使用解决方案资源管理器将 WebForm1.aspx 重命名为 Logon.aspx
• 将表 1 中列出的控件添加到 Logon.aspx 中来创建简单的登录窗体。
表 1:Logon.aspx 控件控件类型文本ID
Label
User Name:
-
Label
Password
-
Text Box
-
txtUserName
Text Box
-
txtPassword
Button
Register
btnRegister
Button
Logon
btnLogon
Label
-
lblMessage
您的 Web 页应与图 1 中所示的页类似。
图 1. 登录页 Web 窗体
• 将 txtPassword 的 TextMode 属性设置为 Password。
配置 Web 应用程序进行 Forms 身份验证
此过程编辑应用程序的 Web.config 文件来配置应用程序以进行 Forms 身份验证。
要配置 Web 应用程序以进行 Forms 身份验证,请执行下列步骤:
1. 使用解决方案资源管理器打开 Web.config。
2. 定位到 元素并将 mode 属性更改为 Forms。
3. 将下列 元素作为 元素的子元素进行添加,并设置 loginUrl、name、timeout 和 path 属性,如下所示:
ms loginUrl="logon.aspx" name="sqlAuthCookie" timeout="60" path="/">
4. 将下列 元素添加到 元素下这一步的目的是只允许经过身份验证的用户访问应用程序。以前建立的 元素的 loginUrl 属性将未经过身份验证的请求重定向到 logon.aspx 页。
开发生成哈希和 Salt 值的函数
此过程向 Web 应用程序添加两个实用工具方法;一个方法生成一个随机 salt 值,另一个方法根据提供的密码和 salt 值创建哈希。
要开发生成哈希和 salt 值的函数,请执行下列步骤:
1. 打开 Logon.aspx.cs 并将下列 using 语句添加到位于文件顶部的现有 using 语句下。
using System.Security.Cryptography;
using System.Web.Security;
2. 将下列静态方法添加到 WebForm1 类中,用于生成随机 salt 值并作为 Base 64 编码字符串返回此值。
private static string CreateSalt(int size)
{
// Generate a cryptographic random number using the cryptographic
// service provider
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] buff = new byte[size];
rng.GetBytes(buff);
// Return a Base64 string representation of the random number
return Convert.ToBase64String(buff);
}
3. 添加下列静态方法以根据提供的密码和 salt 值生成哈希值。
private static string CreatePasswordHash(string pwd, string salt)
{
string saltAndPwd = String.Concat(pwd, salt);
string hashedPwd =
FormsAuthentication.HashPasswordForStoringInConfigFile(
saltAndPwd, "SHA1");
return hashedPwd;
}
创建用户帐户数据库
此过程在 SQL Server 中创建一个新的用户帐户数据库,此数据库包含一个用户表和一个用于查询用户数据库的存储过程。
要创建用户帐户数据库,请执行下列操作:
1. 在 Microsoft SQL Server programs 菜单上,单击 Query Analyzer,然后连接到本地 SQL Server。
2. 输入下列 SQL 脚本。注意,必须用自己的计算机名称替换此脚本末尾的“LocalMachine”。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Magnet link is a link method for downloading resources, which is more convenient and efficient than traditional download methods. Magnet links allow you to download resources in a peer-to-peer manner without relying on an intermediary server. This article will introduce how to use magnet links and what to pay attention to. 1. What is a magnet link? A magnet link is a download method based on the P2P (Peer-to-Peer) protocol. Through magnet links, users can directly connect to the publisher of the resource to complete resource sharing and downloading. Compared with traditional downloading methods, magnetic

How to use mdf files and mds files With the continuous advancement of computer technology, we can store and share data in a variety of ways. In the field of digital media, we often encounter some special file formats. In this article, we will discuss a common file format - mdf and mds files, and introduce how to use them. First, we need to understand the meaning of mdf files and mds files. mdf is the extension of the CD/DVD image file, and the mds file is the metadata file of the mdf file.

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Get started easily: How to use pip mirror source With the popularity of Python around the world, pip has become a standard tool for Python package management. However, a common problem that many developers face when using pip to install packages is slowness. This is because by default, pip downloads packages from Python official sources or other external sources, and these sources may be located on overseas servers, resulting in slow download speeds. In order to improve download speed, we can use pip mirror source. What is a pip mirror source? To put it simply, just
