ASP.NET(c#)語音驗證碼製作

來源:互聯網
上載者:User

最近發現語音驗證碼越來越流行,比如有次在註冊gmail郵箱看到過,還有msn頁面也有語音驗證碼,還有國外一些網站等。
花時間研究了下,語音驗證碼主要跟一般驗證碼的區別就在於如何讓驗證碼播放。本文語音驗證碼原理:從伺服器產生驗證碼,
並儲存到cookie中(getcode.aspx.cs),當點收聽驗證碼的時候,調用javascirpt操作(這裡使用jquery)cookie讀取驗證碼,
然後把驗證碼傳到codevoice.aspx頁,然後按順序把驗證碼合成產生一個mp3檔案,最後把這個檔案傳入flash中播放,
你將收聽的聲音為:“當前驗證碼是5678請輸入”。這個原理也是大部分網站使用的語音驗證碼原理類似。
源碼下載:下載(請使用VS2008 SP1或VS2010開啟)
頁面上放置驗證碼圖片頁面代碼

<form id="form1" runat="server">
    <div>
     <input  type="text" name="txtCode" id="txtCode" maxlength="8"  />
<img onclick="this.src='getcode.aspx';" src="getcode.aspx" align="absmiddle" style="cursor: pointer" alt="看不清楚,換一張" title="看不清楚,換一張" />
<img id="imgRead" src="image/maintb.gif" align="absmiddle" style="cursor: pointer" alt="收聽驗證碼" title="收聽驗證碼" onclick="playvoice('player');" />
<span id="player"></span> 
    </div>
    </form>

點收聽驗證碼時調用的js函數如下:

function playvoice(id) {
     var voiceid = document.getElementById(id);
     var voicecode = $.cookie('ValidateCode');
     voiceid.innerHTML = "<embed id='sound_play' name='sound_play' src='sound_play.swf?" + (new Date().getTime()) + "' 
 FlashVars='isPlay=1&url=codevoice.aspx&code=" + voicecode + "' width='0' height='0' allowScriptAccess='always' 
type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' /></embed>";
 }

 
其中$.cookie('ValidateCode')是讀取cookie驗證碼,這裡使用了一個jquery操作cookie外掛程式

產生mp3頁面代碼如下:
     //讀取驗證碼產生mp3,這裡包括頭部begin.mp3和尾部end.mp3

 Response.ContentType = "audio/mpeg";
            Response.WriteFile("sound/begin.mp3");
            string checkCode = HttpContext.Current.Request.QueryString["code"].ToString();// string checkCode ="8888";
            if (checkCode.Length > 0)
                for (int i = 0; i < checkCode.Length; i++)
                {
                    Response.WriteFile("sound/"+checkCode[i] + ".mp3");
                }
            Response.WriteFile("sound/end.mp3");

           
【本文作者分別在cnblogs,csdn,http://www.ajaxcn.net/同步發布,轉載請保留此說明】
flash播放代碼主要在第一幀主要畫面格右擊動作,插入以下代碼根據傳入的播放數字mp3地址

var mysound = new Sound();
var mysong = url;
var isPlay = 1;
var intnum:Number = setInterval(playSong, 500);
function playSong() {
 if (isPlay == 1) {
  mysound.loadSound(mysong+"?code="+code, true);
  mysound.start();
  clearInterval(intnum);
  isPlay = 0;
 }
}

另外特別說明:此源碼參考了開源CMS中的登入頁:JumbotCms

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.