ajax短信验证码-mvc

<script type="text/javascript">
function SendMessage() {
var phoneNumberInput = document.getElementById("TelephoneNumber");
var phoneNumber = phoneNumberInput.value;
$.get("/Sms/WebSendMessage",
"phoneNumber=" + phoneNumber + "&identity="+@identity);
}
</script>

转载于:https://www.cnblogs.com/maoyazhi/p/6045647.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
阿里云短信验证码可以实现手机验证、身份验证等功能,非常适用于需要短信验证的场景。下面是一个动态MVC的实战例子: 1. 首先,在控制器中添加以下代码,用于生成随机验证码并发送短信。 ```csharp public ActionResult SendVerifyCode(string phone) { // 生成6位随机验证码 string verifyCode = new Random().Next(100000, 999999).ToString(); // 将验证码存入Session中,用于后续验证 Session["VerifyCode"] = verifyCode; // 调用阿里云短信API,发送验证码短信 string result = SendSms(phone, verifyCode); // 返回发送短信结果 return Content(result); } ``` 2. 在发送短信的方法中,调用阿里云短信API,实现短信发送功能。 ```csharp private string SendSms(string phone, string verifyCode) { string appKey = "your_app_key"; string appSecret = "your_app_secret"; string signName = "your_sign_name"; string templateCode = "your_template_code"; IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", appKey, appSecret); DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.Method = MethodType.POST; request.Domain = "dysmsapi.aliyuncs.com"; request.Version = "2017-05-25"; request.Action = "SendSms"; request.AddQueryParameters("PhoneNumbers", phone); request.AddQueryParameters("SignName", signName); request.AddQueryParameters("TemplateCode", templateCode); request.AddQueryParameters("TemplateParam", "{\"code\":\"" + verifyCode + "\"}"); try { CommonResponse response = client.GetCommonResponse(request); return response.Data; } catch (ServerException e) { return e.Message; } catch (ClientException e) { return e.Message; } } ``` 3. 在前端页面中,添加以下代码,用于调用控制器中的发送验证码方法。 ```html <input type="text" id="phone" name="phone" /> <button onclick="sendVerifyCode()">发送验证码</button> <script> function sendVerifyCode() { var phone = $("#phone").val(); $.ajax({ url: "/ControllerName/SendVerifyCode?phone=" + phone, success: function (result) { alert(result); } }); } </script> ``` 4. 最后,在控制器中添加以下代码,用于验证用户输入的验证码是否正确。 ```csharp public ActionResult VerifyCode(string code) { string verifyCode = Session["VerifyCode"].ToString(); if (code == verifyCode) { return Content("验证通过"); } else { return Content("验证失败"); } } ``` 在前端页面中,添加以下代码,用于调用控制器中的验证码验证方法。 ```html <input type="text" id="code" name="code" /> <button onclick="verifyCode()">验证</button> <script> function verifyCode() { var code = $("#code").val(); $.ajax({ url: "/ControllerName/VerifyCode?code=" + code, success: function (result) { alert(result); } }); } </script> ``` 以上就是一个简单的阿里云短信验证码实战动态MVC的例子,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值