ASP. NET MVC Access SMS Verification code sent

Source: Internet
Author: User

1th Step: Log in to the US-Saint SMS Console

Http://www.rcscloud.cn/hy/HY_ZH/login

account Number:*******

Password:*******

Http://www.rcscloud.cn/common/API

Step 2 : Add account signatures and SMS Templates

Step 3 : Download the template

4th Step: Create a new project in your project gmkcollege.rcscloud and put two files in App_Code into your project, notice that you modify the namespace

5th Step: Front End

<tr class= "Margin-top" >

<TD class= "Padding-top text-center" > Mobile phone number </td>

<td><input type= "text" class= "inputs" id= "phone" name= "Phone" > </td>

<td><input type= "button" value= "Get Verification Code" id= "SMS" onclick= "SendEmail ()" ></td>

</tr>

<tr>

<TD class= "Padding-top text-center" > Verification Code </td>

<td><input type= "text" class= "inputs" id= "code" name= "code" ></td>

</tr>

Step 6 :js processing

$ (function () {

$ ("#sms"). Click (function () {

Sendcode ($ ("#sms"));

});

v = getcookievalue ("secondsremained");//Get Cookie value

if (V > 0) {

SetTime ($ ("#sms"));//Start Countdown

}

})

Send Verification Code

function Sendcode (obj) {

var PhoneNumber = $ ("#Phone"). Val ();

var result = Isphonenum (PhoneNumber);

if (result) {

Sending a mobile phone using AJAX to the backend text messaging interface

$.post ("/college/code", {Phone:phonenumber}, function (data) {

if (data = = "OK") {

Alert ("Verification code sent successfully!");

} else {

Alert ("The verification code sent failed, please resend!");

}

});

Addcookie ("secondsremained", 60, 60);//Add cookie record, effective time 60s

SetTime (obj); Start the countdown.

}

}

Start the countdown.

var countdown;

function SetTime (obj) {

Countdown = Getcookievalue ("secondsremained");

if (countdown = = 0) {

Obj.removeattr ("Disabled");

Obj.val ("Get Verification Code");

Return

} else {

Obj.attr ("Disabled", true);

Obj.val ("Resend (" + Countdown + ")");

countdown--;

Editcookie ("secondsremained", Countdown, Countdown + 1);

}

SetTimeout (function () {settime (obj)}, 1000)//every 1000 milliseconds

}

Verifying the phone number is legal

function Isphonenum (phonenumber) {

var Myreg =/^ ((13[0-9]{1}) | ( 15[0-9]{1}) | (18[0-9]{1})) +\D{8}) $/;

if (!myreg.test (PhoneNumber)) {

Alert (' Please enter a valid mobile number! ‘);

return false;

} else {

return true;

}

}

Add a cookie when sending a verification code

function Addcookie (name, value, expireshours) {

var cookiestring = name + "=" + escape (value);

Determine if the expiration time is set, 0 is invalid when the browser is closed

if (expireshours > 0) {

var date = new Date ();

Date.settime (Date.gettime () + expireshours * 1000);

cookiestring = cookiestring + "; expires=" + date.toutcstring ();

}

Document.cookie = cookiestring;

}

Modify the value of a cookie

function Editcookie (name, value, expireshours) {

var cookiestring = name + "=" + escape (value);

if (expireshours > 0) {

var date = new Date ();

Date.settime (Date.gettime () + expireshours * 1000); Unit is milliseconds

cookiestring = cookiestring + "; expires=" + date.togmtstring ();

}

Document.cookie = cookiestring;

}

Get the value of a cookie by name

function Getcookievalue (name) {

var strcookie = Document.cookie;

var Arrcookie = Strcookie.split (";");

for (var i = 0; i < arrcookie.length; i++) {

var arr = arrcookie[i].split ("=");

if (arr[0] = = name) {

Return unescape (arr[1]);

Break

} else {

Return "";

Break

}

}

}

7th Step: Processing in the background controller

US-san Melt cloud Verification code sent

Public ActionResult Validatecode ()

{

String Code = getrandomstring (6);

Send Template SMS

string result = Rcscloudrestapi.sendtplsms (

"F3b70fc99e1040b99657da1f9411df05",

request["Phone"],

"@[email protected]=" +code,

""

);

To set the verification code to cache

var codeinfo = (Object) Code;

Cacheopt.setcache ("Code", Codeinfo, Convert.ToInt32 (60));

return Json (Result);

}

#region generate 6-bit verification code

public string getrandomstring (int ilength)

{

String buffer = "0123456789"; A random character can also be a Chinese character (any)

StringBuilder sb = new StringBuilder ();

Random r = new Random ();

int range = buffer. Length;

for (int i = 0; i < ilength; i++)

{

Sb. Append (buffer. Substring (R.next (range), 1));

}

Return SB. ToString ();

}

#endregion

Step 8 : Cache processing

public class Cacheopt

{

<summary>

Setting up the cache

</summary>

<param name= "CacheKey" ></param>

<param name= "Objobject" ></param>

<param name= "Seconds" > Expires after how many seconds </param>

public static void Setcache (String CacheKey, Object Objobject, Long Seconds)

{

System.Web.Caching.Cache Objcache = Httpruntime.cache;

Objcache.insert (CacheKey, objobject, NULL, System.DateTime.Now.AddSeconds (Seconds), TimeSpan.Zero);

}

<summary>

Get data cache

</summary>

<param name= "CacheKey" > Keys </param>

public static Object GetCache (String CacheKey)

{

System.Web.Caching.Cache Objcache = Httpruntime.cache;

return Objcache[cachekey];

}

}

ASP. NET MVC Access SMS Verification code sent

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.