国产探花免费观看_亚洲丰满少妇自慰呻吟_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="";
   }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陆良县| 广德县| 玉龙| 井冈山市| 寻乌县| 黄陵县| 克东县| 灵宝市| 和林格尔县| 台前县| 碌曲县| 毕节市| 潜山县| 新昌县| 凌云县| 宝清县| 日喀则市| 十堰市| 北票市| 甘孜县| 上杭县| 台北县| 宾阳县| 遵义县| 元朗区| 扎鲁特旗| 堆龙德庆县| 开封县| 虞城县| 阳信县| 汨罗市| 怀宁县| 花莲县| 连州市| 揭东县| 道孚县| 玉林市| 株洲县| 通江县| 水城县| 南靖县|