个人网站实现扫码登录asp.net 扫码登录
2016-04-03 11:47
716 查看
ok,看看源码吧:
后台处理:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Asxsyd92.Entity;
using System.Web.SessionState;
namespace 爱上歆随懿恫.asxsyd92
{
/*===========================================================================
// Copyright (C) 2016 爱上歆随懿恫
// 作者: 爱上歆随懿恫 邮箱:asxsyd92@foxmail.com qq:1316227882
// 功能描述:登录功能
// 更新日期:2016年3月28日 更新人:陈良富 更新原因/内容
//
===========================================================================*/
public class Handler1 : IHttpHandler, IRequiresSessionState
{
System.Web.Script.Serialization.JavaScriptSerializer jsS = new System.Web.Script.Serialization.JavaScriptSerializer();
string result = string.Empty;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string type = context.Request["type"];
if (string.IsNullOrEmpty(type))
{
context.Response.Write(result = jsS.Serialize(new { Success = false, data = "", msg = "无效请求!" }));
}
else {
switch (type) {
case "login": Login(context); break;
case "reg": Reg(context); break;
case "creatqrcde": Qrcode(context); break;
case "check": CheckQrcode(context); break;
case "appcheck": AppCheckQrcode(context); break;
default: break;
}
}
}
#region+手机端扫描
/// <summary>
/// 手机端扫描验证
/// </summary>
/// <returns></returns>
private void AppCheckQrcode(HttpContext context)
{
var user = context.Request["user"];
var u_qrcde = context.Request["u_qrcde"];
if (string.IsNullOrEmpty(user) && string.IsNullOrEmpty(u_qrcde))
{
context.Response.Write(result = jsS.Serialize(new { msg = "无效请求", Success = false }));
}
else {
Asxsyd92.Entity.login d = new login();
if (d.Update(u_qrcde,user))
{
context.Response.Write(result = jsS.Serialize(new { msg = "扫描成功", Success = true, User = user, Qrcode = u_qrcde }));
}
else {
context.Response.Write(result = jsS.Serialize(new { msg = "扫描异常", Success = false }));
}
}
}
#endregion
#region+注册模块
/// <summary>
/// 注册模块
/// </summary>
/// <returns></returns>
private void Reg(HttpContext context)
{
try
{
string user = context.Request["reguser"];
string pw = context.Request["regpw"];
if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pw))
{
context.Response.Write(result = jsS.Serialize(new { Success = false, data = "", msg = "用户名或者密码不能为空!" }));
}
else
{
Asxsyd92.Entity.login d = new login();
if (d.goreg(user, pw))
{
context.Response.Write(result = jsS.Serialize(new { msg = "恭喜成功!", Success = true }));
}
else
{
context.Response.Write(result = jsS.Serialize(new { msg = "对不起,公测版禁止注册请以测试用户登录,用户名和密码admin", Success = false }));
}
}
}
catch (Exception e)
{
context.Response.Write(result = jsS.Serialize(new { msg = e.Message, Success = false }));
}
}
#endregion
#region+登录模块
/// <summary>
/// 登录模块
/// </summary>
/// <returns></returns>
public void Login( HttpContext context) {
try
{
string user = context.Request["user"];
string pw = context.Request["pw"];
if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pw))
{
context.Response.Write(result = jsS.Serialize(new { Success = false, data = "", msg = "用户名或者密码不能为空!" }));
}
else
{
Asxsyd92.Entity.login d = new login();
if (d.gologin(user.ToString().Trim(), pw.ToString().Trim()))
{
System.Web.HttpContext.Current.Session["user"] = user.ToString().Trim();
context.Response.Write(result = jsS.Serialize(new { msg = "登录成功!", Success = true }));
}
else
{
context.Response.Write(result = jsS.Serialize(new { msg = "用户名或者密码!", Success = false }));
}
}
}
catch (Exception e) {
context.Response.Write(result = jsS.Serialize(new { msg = e.Message, Success = false }));
}
}
#endregion
#region+生成二维码字符串
/// <summary>
/// 生成二维码字符串
/// </summary>
/// <returns></returns>
public void Qrcode(HttpContext context) {
Asxsyd92.Tools.Tool.Token t = new Asxsyd92.Tools.Tool.Token();
Asxsyd92.Entity.login d = new login();
string qrcode = t.Create().ToString().ToLower();
d.AddQrcde(qrcode);
context.Session["qrcode"] = qrcode;
context.Response.Write(result = jsS.Serialize(new { msg = "获取成功!", Success = true, Qrcode = qrcode }));
}
#endregion
#region+循环检查手机登录状态
/// <summary>
/// +循环检查手机登录状态
/// </summary>
/// <returns></returns>
public void CheckQrcode(HttpContext context)
{
Asxsyd92.Entity.login d = new login();
if (context.Session["qrcode"] == null)
{
context.Response.Write(result = jsS.Serialize(new { msg = "验证码失效!", Success = false }));
}
else
{
var ks = d.UserQrcdeLogin(context.Session["qrcode"].ToString());
if (ks != null)
{
context.Session["user"] = ks;
context.Response.Write(result = jsS.Serialize(new { msg = "登录成功", Success = t
c0fb
rue, user = ks }));
}
else
{
context.Response.Write(result = jsS.Serialize(new { msg = "请用手机扫描二维码", Success = false }));
}
}
}
#endregion
public bool IsReusable
{
get
{
return false;
}
}
}
}
后台包含pc端和手机端看看源码就知道了, 手机端获取二维码代码片段:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SCANNIN_GREQUEST_CODE:
if(resultCode == RESULT_OK){
Bundle bundle = data.getExtras();
//显示扫描到的内容
mTts.startSpeaking("正在处理请稍等。", mTtsListener);
webView.loadUrl("http://182.254.140.26/app/weblogin.html?id="+bundle.getString("result"));
}
break;
}
}
接下来看看WebApp处理代码(片段)
<script>
$(function () {
if (window.sessionStorage["user"] == null || window.sessionStorage["user"] == undefined) {
window.location.href = "login1.html";
} else {
$.ajax({
type: "Post",
url: "../asxsyd92/ashx/Login.ashx",
data: { type: "appcheck", u_qrcde: GetQueryString("id"), user: window.sessionStorage["user"] },
dataType: "json",
success: function (data) {
if (data.Success) {
show_msg(data.msg, "index.html");
}
else {
show_err_msg(data.msg);
}
},
error: function () {
show_err_msg('请求超时!');
}
});
}
});
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
</script>
pc端生的处理代码:
$(function () {
$('#_qrcode').click(function () {
if ($('#zy').val() == "zh") {
$('#login_form').hide();
$("#qrcodeTable").show();
$('#zy').val('er');
$('#_qrcode').addClass('btn-account');
$('#_qrcode').removeClass('btn-code');
loingshow("正在搬运二维码");
$.ajax({
type: "Post",
url: "../../asxsyd92/ashx/Login.ashx",
data: { type: "creatqrcde" },
dataType: "json",
success: function (data) {
if (data.Success) {
asxsyd92.createQrcode(data.Qrcode);
$('.msg_bg').remove();
$('.sub_err').remove();
check();
}
else {
show_err_msg(data.msg);
}
},
error: function () {
show_err_msg('请求超时!');
}
});
//
} else {
$("#qrcodeTable").hide();
$('#login_form').show();
$('#zy').val('zh');
$('#_qrcode').addClass('btn-code');
$('#_qrcode').removeClass('btn-account');
}
});
if (window.sessionStorage["user"] != null || window.sessionStorage["user"] != undefined) {
show_msg('您已登录! 正在为您跳转...', "/");
} else {
//提交表单
$('#submit_btn').click(function () {
//var myReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; //邮件正则
//if ($('#email').val() == '') {
// show_err_msg('用户名不能为空!');
// $('#email').focus();
//}else
//var inputCode = document.getElementById("inputCode").value;
//if (inputCode.length <= 0) {
// alert("请输入验证码!");
//}
//else if (inputCode.toUpperCase() != code.toUpperCase()) {
// alert("验证码输入有误!");
// createCode();
//}
//else {
// alert("验证码正确!");
//}
if ($('#email').val() == '') {
show_err_msg('用户名不能为空!');
$('#email').focus();
} else if ($('#password').val() == '') {
show_err_msg('密码还没填呢!');
$('#password').focus();
} else if ($('#j_captcha').val() == '') {
show_err_msg('请输入验证码!');
$('#j_captcha').focus();
asxsyd92.createCode();
}
else {
if ($('#j_captcha').val().toUpperCase() == window.sessionStorage["code"].toUpperCase()) {
loingshow("亲!正在为您拼命加载,请稍等。。。");
$.ajax({
type: "Post",
url: "../../asxsyd92/ashx/Login.ashx",
data: { user: $('#email').val(), pw: $('#password').val(), type: "login" },
dataType: "json",
success: function (data) {
$('.msg_bg').remove();
$('.sub_err').remove();
if (data.Success) {
window.sessionStorage["user"] = $('#email').val();
sessionStorage.removeItem("code");
show_msg('登录成功咯! 正在为您跳转...', "/");
} else {
show_err_msg(data.msg);
asxsyd92.createCode();
}
},
error: function () {
show_err_msg('请求超时!');
sessionStorage.removeItem("code");
asxsyd92.createCode();
}
});
} else {
show_err_msg('验证码错误!');
sessionStorage.removeItem("code");
asxsyd92.createCode();
}
}
});
$('#submit_btn_reg').click(function () {
if ($('#user').val() == '') {
show_err_msg('用户名不能为空!');
$('#user').focus();
} else if ($('#pw').val() == '') {
show_err_msg('密码还没填呢!');
$('#pw').focus();
} else if ($('#npw').val() == '') {
show_err_msg('密码还没填呢!');
$('#npw').focus();
}
else if ($('#pw').val() != $('#npw').val()) {
show_err_msg('亲!两次输入密码不一致!');
}
else {
loingshow("亲!正在为您注册,请稍等片刻。。。");
$.ajax({
type: "Post",
url: "../../asxsyd92/ashx/Login.ashx",
data: { reguser: $('#user').val(), regpw: $('#pw').val(), type: "reg" },
dataType: "json",
success: function (data) {
$('.msg_bg').remove();
$('.sub_err').remove();
if (data.Success) {
show_err_msg(data.msg);
$('#regModal').modal('hide');
} else {
show_err_msg(data.msg);
}
},
error: function () {
show_err_msg('请求超时!');
}
});
}
});
}
});
var tz;
function check() {
$.ajax({
type: "Post",
url: "../../asxsyd92/ashx/Login.ashx",
data: { type: "check" },
dataType: "json",
success: function (data) {
if (data.Success) {
window.sessionStorage["user"] = data.user;
show_msg('扫描成功正在为您登录...', "/");
// alert(data.msg);
clearInterval(tz);
}
else {
tz = setInterval(check, 5000);
// show_err_msg(data.msg);
}
},
error: function () {
// show_err_msg('请求超时!');
}
});
};
function tingzhi() {
alert("停止");
clearInterval(tz);
}
setTimeout(tingzhi, 1000 * 60 * 2);
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 星外ASP.Net的安全设置相关说明
- C#、ASP.NET通用扩展工具类之TypeParse
- 实现ASP.NET无刷新下载并提示下载完成的开发思路
- C#、ASP.NET通用扩展工具类之LogicSugar
- C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)
- 程序中常用的种代码
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- ASP.NET页面间的传值的几种方法
- 入侵ASP.net网站的经验附利用代码
- 盘点PHP和ASP.NET的10大对比!
- asp.net OleDbCommand 的用法
- jquery调用asp.net 页面后台的实现代码
- asp.net中XML如何做增删改查操作
- asp.net数据验证控件
- asp.net中的check与uncheck关键字用法解析
- ASP.NET MVC 4 捆绑和缩小实例介绍
- ASP.Net Post方式获取数据流的一种简单写法
- asp.net动态产生checkbox(数据源为DB或内存集合)
- Asp.Net 和 AJAX.Net 的区别第1/2页