首先本次需求是在MVC5中进行的,通过收费的融云服务来验证手机号码,而且本次的项目也是前后台分离,所以添加了WEBAPI2,那么先添加WEBAPI的接口

using System;
using System.Web.Http;
using System.Net.Http;
using System.Web.Script.Serialization;
using System.Text;
using System.Web; namespace BlackNails.Controllers
{
public abstract class WebAPI2BaseController : ApiController
{
public HttpContext context = HttpContext.Current; public static HttpResponseMessage toJson(Object obj)
{
String str;
if (obj is String || obj is Char)
{
str = obj.ToString();
}
else
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
str = serializer.Serialize(obj);
}
HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
} public string Options()
{
return null; // HTTP 200 response with empty body
}
}
}

然后添加一个UserController接口文档

using BlackNails.CommonClass;
using BlackNails.Controllers;
using BlackNails.DAL;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
using System.Web.Script.Serialization; namespace BlackNails.WebAPI
{
public class UserController : WebAPI2BaseController
{ [HttpGet]
public HttpResponseMessage sendCode(string mobile)
{
var response = new Response(); Random r = new Random();
int i = r.Next(, );
string Random = i.ToString(); TimeSpan ts = DateTime.Now - DateTime.Parse("1970-1-1");
string Timestamp = Convert.ToInt32(ts.TotalSeconds).ToString(); //以字节方式存储
byte[] data = Encoding.Default.GetBytes(Constant.APP_SECRET + Random + Timestamp);
System.Security.Cryptography.SHA1 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
//得到哈希值
byte[] result = sha1.ComputeHash(data);
//转换成为字符串的显示
string Signature = BitConverter.ToString(result).Replace("-", ""); WebRequest request = WebRequest.Create("http://api.sms.ronghub.com/sendCode.json");
request.Method = "POST";
string postData = "mobile=" + mobile + "&templateId=" + Constant.TEMPLATE_REGISTER + "&region=86";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
request.Headers.Add("App-Key", Constant.APP_KEY);
request.Headers.Add("Nonce", Random);
request.Headers.Add("Timestamp", Timestamp);
request.Headers.Add("Signature", Signature);
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, , byteArray.Length);
dataStream.Close();
WebResponse resp = request.GetResponse();
dataStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
ReturnJson _ReturnJson = jsonSerializer.Deserialize<ReturnJson>(responseFromServer);
var code = _ReturnJson.code;
var sessionId = _ReturnJson.sessionId;
if(code == )
{
response.Code = ;
response.Message = "发送验证码成功!";
context.Cache.Insert(mobile, sessionId);
} else
{
response.Code = ;
response.Message = "发送验证码失败!";
context.Cache.Insert(mobile, "");
}
reader.Close();
dataStream.Close();
resp.Close(); response.Data = null;
return toJson(response);
} [HttpGet]
public HttpResponseMessage verifyCode(string mobile, string code)
{
var response = new Response(); Random r = new Random();
int i = r.Next(, );
string Random = i.ToString(); TimeSpan ts = DateTime.Now - DateTime.Parse("1970-1-1");
string Timestamp = Convert.ToInt32(ts.TotalSeconds).ToString(); //以字节方式存储
byte[] data = Encoding.Default.GetBytes(Constant.APP_SECRET + Random + Timestamp);
System.Security.Cryptography.SHA1 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
//得到哈希值
byte[] result = sha1.ComputeHash(data);
//转换成为字符串的显示
string Signature = BitConverter.ToString(result).Replace("-", ""); WebRequest request = WebRequest.Create("http://api.sms.ronghub.com/verifyCode.json");
request.Method = "POST";
string postData = "sessionId="+ context.Cache[mobile] +"&code=" + code;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("App-Key", Constant.APP_KEY);
request.Headers.Add("Nonce", Random);
request.Headers.Add("Timestamp", Timestamp);
request.Headers.Add("Signature", Signature);
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, , byteArray.Length);
dataStream.Close();
WebResponse resp = request.GetResponse();
dataStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
ReturnJson _ReturnJson = jsonSerializer.Deserialize<ReturnJson>(responseFromServer);
var rtnCode = _ReturnJson.code;
var rtnSuccess = _ReturnJson.success;
if (rtnCode == )
{
response.Code = ;
response.Message = "手机号验证成功!";
}
else
{
response.Code = ;
response.Message = "手机号验证失败!";
}
reader.Close();
dataStream.Close();
resp.Close(); response.Data = rtnSuccess;
return toJson(response);
}
}
}
04-26 18:58