Password protection in ASP. NET, use of MD5 and SHA1 Algorithms

Source: Internet
Author: User
Tags password protection

Your homepage or website you manage has various passwords that need to be protected. There are many security risks when you directly store passwords in databases or files. Therefore, encrypted password storage is the most common practice. Encryption in ASP. NET is very easy .. . Net sdk provides the CookieAuthentication class. The HashPasswordForStoringInConfigFile method can directly use the MD5 and SHA1 algorithms. Example: <br>
File: encrypting. aspx <br>
<% @ Page language = "c #" Codebehind = "encrypting. cs" AutoEventWireup = "false" Inherits = "encrypting. encrypting" %> <br>
<Html> <Meta name = "GENERATOR" Content = "Microsoft Visual Studio 7.0"> <br>
<Meta name = "CODE_LANGUAGE" Content = "C #"> <Body> <br>
<Br>
<Form method = "post" runat = "server"> <br>
<P> </p> <br>
<P> <br>
<Asp: TextBox id = TextBox1 runat = "server"> </asp: TextBox> <br>
<Asp: Button id = Button1 runat = "server" Text = "encrypting"> </asp: Button> </p> <br>
<P> Encrypting Password (MD5): <br>
<Asp: Label id = MD5 runat = "server"> </asp: Label> </p> <br>
</Form> <br>
<Br>
</Body> <Br>
File: encrypting. cs <br>
<Br>
Namespace encrypting <br>
{<Br>
Using System; <br>
Using System. Collections; <br>
Using System. ComponentModel; <br>
Using System. Data; <br>
Using System. Drawing; <br>
Using System. Web; <br>
Using System. Web. SessionState; <br>
Using System. Web. UI; <br>
Using System. Web. UI. WebControls; <br>
Using System. Web. UI. HtmlControls; <br>
Using System. Web. Security; <br>
/// <Summary> <br>
/// Summary description for encrypting. <br>
/// </Summary> <br>
Public class encrypting: System. Web. UI. Page <br>
{<Br>
Protected System. Web. UI. WebControls. Label MD5; <br>
Protected System. Web. UI. WebControls. Button Button1; <br>
Protected System. Web. UI. WebControls. TextBox TextBox1; <br>
<Br>
Public encrypting () <br>
{<Br>
Page. Init + = new System. EventHandler (Page_Init); <br>
} <Br>
Protected void Page_Load (object sender, EventArgs e) <br>
{<Br>
If (! IsPostBack) <br>
{<Br>
// <Br>
// Evals true first time browser hits the page <br>
// <Br>
} <Br>
} <Br>
Protected void Page_Init (object sender, EventArgs e) <br>
{<Br>
// <Br>
// CODEGEN: This call is required by the ASP + Windows Form Designer. <br>
// <Br>
InitializeComponent (); <br>
} <Br>
/// <Summary> <br>
/// Required method for Designer support-do not modify <br>
/// The contents of this method with the code editor. <br>
/// </Summary> <br>
Private void InitializeComponent () <br>
{<Br>
Button1.Click + = new System. EventHandler (this. button#click); <br>
This. Load + = new System. EventHandler (this. Page_Load); <br>
} <Br>
Public void button#click (object sender, System. EventArgs e) <br>
{<Br>
MD5.Text = CookieAuthentication. HashPasswordForStoringInConfigFile (TextBox1.Text, "MD5"); <br>
// SHA1 use CookieAuthentication. HashPasswordForStoringInConfigFile (TextBox1.Text, "SHA1"); <br>
} <Br>
} <Br>
} <Br>
Note: The namespace of CookieAuthentication class is System. Web. Security.

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.