The use of password protection, MD5 and SHA1 algorithms in ASP.NET

xiaoxiao2021-03-05  24

Your home page or you manage the website has a variety of passwords to be protected, put your password directly in a database or a lot of security hazards, so password encrypted storage is the most common practice. It is very easy to achieve encryption in ASP.NET. The CookieAuthentication class is available in the .NET SDK, where the HashPasswordforstoringInConfigFile method can use the MD5 and SHA1 algorithms directly. The example is as follows:

FILE: Encrypting.ASPX

<% @ Page language = "c #" codebehind = "encrypting.cs" autoeventwireup = "false" inherits = "encrypting.encrypting"%>

FILE: Encrypting.cs

Namespace encrypting

{

Using system;

Using system.collections;

Using system.componentmodel;

Using system.data;

Using system.drawing;

Using system.Web;

Using system.Web.SessionState;

Using system.Web.ui;

Using system.Web.ui.webcontrols;

Using system.Web.ui.htmlcontrols;

Using system.web.security;

///

/// summary description for encrypting. ///

Public class encrypting: system.web.ui.page

{

protected system.web.ui.webcontrols.label MD5;

protected system.web.ui.webcontrols.button button1;

protected system.web.ui.webcontrols.textbox textbox1;

Public EncryPTING ()

{

Page.init = New System.EventHandler (Page_init);

}

Protected void Page_load (Object Sender, Eventargs E)

{

IF (! ispostback)

{

//

// Evals True First Time Browser Hits The PAGE

//

}

}

Protected Void Page_init (Object Sender, Eventargs E)

{

//

// Codegen: This Call is Required by The ASP Windows Form Designer.

//

InitializationComponent ();

}

///

/// Required Method for Designer Support - Do Not Modify /// The Contents of this Method with the code editor. ///

Private vidinitiRizeComponent ()

{

Button1.click = new system.eventhandler (this.button1_click);

This.Load = New System.EventHandler (this.page_load);

}

Public void Button1_Click (Object Sender, System.Eventargs E)

{

Md5.text = cookieauthentication.hashPasswordforstoringInconfigfile (TextBox1.text, "MD5"; // Sha1 Use CookieAuthentication.hashPasswordforstoringInfigfile (TextBox1.text, "SHA1";

}

}

}

Note: Namespace of CookieAuthentication is System.Web.Security.

转载请注明原文地址:https://www.9cbs.com/read-38011.html

New Post(0)