国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

使用 ASP.NET 加密口令

2024-07-10 12:55:51
字體:
來源:轉載
供稿:網友
 

在asp中,并未提供加密的對象,我們只能使用外部的對象來進行加密。現在好了,在asp.net中提供了加密的解決方法。在名字空間system.web.security中包含了類formsauthentication,其中有一個方法hashpasswordforstoringinconfigfile。這個方法可以將用戶提供的字符變成亂碼,然后存儲起來,甚至可以 存儲在cookies中。

  hashpasswordforstoringinconfigfile方法使用起來很簡單,它支持"sha1"和"md5"加密算法。

下面的代碼簡單的演示了關于其用法:

<%@ page language="c#" %>
  <%@ import namespace="system.web.security" %>
  <html>
   <head>
   <script language="c#" runat="server">
   public void encryptstring(object sender, eventargs e)
   {
   sha1.text = formsauthentication.hashpasswordforstoringinconfigfile(txtpassword.text,"sha1");
   md5.text =formsauthentication.hashpasswordforstoringinconfigfile(txtpassword.text, "md5") ;
   }
   </script>
   </head>
   <body>
   <form runat="server" id="form1">
   <p>
   <b>original clear text password: </b>
   <br>
   <asp:textbox id="txtpassword" runat="server" />
   <asp:button runat="server" text="encrypt string" onclick="encryptstring" id="button1" />
   </p>
   <p>
   <b>encrypted password in sha1: </b>
   <asp:label id="sha1" runat="server" />
   </p>
   <p>
   <b>encrypted password in md5: </b>
   <asp:label id="md5" runat="server" />
   </p>
   </form>
   </body>
  </html>

正如你所看到的這樣簡單易用。我們可以把這段加密程序封裝在一個函數里便于重復的使用。代碼如下:

public string encryptpassword(string passwordstring,string passwordformat )
   {
   if (passwordformat="sha1"){
   encryptpassword=formsauthortication.hashpasswordforstoringinconfigfile(passwordstring ,"sha1");
   }
   elseif (passwordformat="md5")
   { encryptpassword=formsauthortication.hashpasswordforstoringinconfigfile(passwordstring ,"md5");
   }
   else
   {
   encryptpassword="";
   }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 轮台县| 施秉县| 兴安盟| 鹤壁市| 诸暨市| 丰台区| 女性| 平邑县| 大荔县| 深泽县| 庆云县| 新昌县| 化德县| 平凉市| 南安市| 镇平县| 商水县| 芜湖市| 昌图县| 舒兰市| 淄博市| 佛学| 兴安盟| 上虞市| 孝感市| 汉沽区| 五河县| 姜堰市| 招远市| 宿迁市| 新源县| 永嘉县| 塔城市| 信宜市| 九江市| 运城市| 清镇市| 伽师县| 兴海县| 东城区| 盐亭县|