码迷,mamicode.com
首页 > 微信 > 详细

C# 开发微信扫码登录

时间:2017-09-13 18:30:32      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:open   反序   linq   err   post   管理   use   doc   连接   

 

一,JS 代码参考

<script type="text/javascript">
function GetRequest() {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
window.onload = function () {
$(".AdminBody").css("display", "none");

var strCookie = document.cookie;
// alert("strCookie=" + strCookie);
if (strCookie == "报表管理员")
{
$(".AdminBody").css("display", "block");
}
else
{
var Code = GetRequest()[‘code‘];
if (Code) {
// alert("Code有值");

//if (strCookie != null && strCookie != "")
//{
// strCookie = strCookie.split(‘=‘)[1];
//}
if (!Code || strCookie) {
Code = strCookie;
}
$.ajax({
type: ‘post‘,
url: "HandelServices/Login.ashx?Code=" + Code,
dataType: "json",
async: false,
success: function (data) {
// alert("Success");
console.log(data);
if (data != null && data != "") {
var result = JSON.parse(data);
var roleName = result[0].RoleName;
var Title = result[0].Title;
$("#HiddUser").val(Title);
document.cookie = roleName
//alert($("#HiddUser").val());
switch (roleName) {
case "报表管理员":
$(".AdminBody").css("display", "block");
break;
}
}
}, error: function () {
//alert("Error");
$(".AdminBody").css("display", "none");
}
});
} else {
// alert("Code无值");
window.location.href = ‘https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=wxf8d1544891f5d3a5&agentid=1000003&redirect_uri=http://wechatapps.smith-nephew.com.cn/salesreport/Admin.html&state=web_login@gyoss9‘;
}
}

}
</script>

 

 

二。后台代码参考(一般处理程序)

using FK.SN.Utilities.Common;
using FK.SN.WeChat.CorpAuthServiceClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FK.SN.WeChat.HandelServices
{
/// <summary>
/// Login 的摘要说明
/// </summary>
public class Login : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string Code = context.Request.Params["Code"].CastToString();
string AppID = ConfigHelper.GetConfigStr("AppID").CastToString();
string AppSecret = ConfigHelper.GetConfigStr("AppSecret").CastToString();
Log.CreateLogManager().Debug("Code=" + Code);
Log.CreateLogManager().Debug("AppID=" + AppID);
Log.CreateLogManager().Debug("AppSecret=" + AppSecret);

string UserAccount = "";

string getTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("corpid", AppID);
parameters.Add("corpsecret", AppSecret);

string GetTokenData = HttpUtils.DoGet(getTokenUrl, parameters);

//反序列化为对象,取得access_token,然后通过access_token和code获得用户账号
// GetTokenData 数据{"errcode":0,"errmsg":"ok","access_token":"Mhi1RW4MTat2XX6miEVUvu1o1ETpm06SAn_WBtT6DClVtp1mCiPIFSt1HWjotGy_sUrmwWI_HZeC8UgH_yN9Y5Rlsxib-hQqEVp4YnY5nNjozfW-r_85aoQgTvXss7A7iR_jU8I-L0-m0ZnCvAdU5uTPXhDUW41IaRJozynI9v-tvEC8LN7LIBwSrWsg75HjJzh3hBHSsg5FLvdHBPKpjZ-b2nzksm3ikc96KUcW2yvokvhaAIFtYKF702eTYs7FDxYbHNDshqyPyqEb7FTWVI1myb1OJwoPCeV1QmlIskE","expires_in":7200}
GetToken list = SerializeHelper.JsonDeSerialize<GetToken>(GetTokenData);
if (list != null)
{
string access_token = list.access_token;
Log.CreateLogManager().Debug("access_token=" + access_token);
string UserUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo";
IDictionary<string, string> parametersUser = new Dictionary<string, string>();
Log.CreateLogManager().Debug("Code=" + Code);
parametersUser.Add("access_token", access_token);
parametersUser.Add("code", Code);

string GetUserAccount = HttpUtils.DoGet(UserUrl, parametersUser);
Log.CreateLogManager().Debug("GetUserAccount=" + GetUserAccount);
//{"UserId":"WangSenSen","DeviceId":"","errcode":0,"errmsg":"ok"}
GetUser listUser = SerializeHelper.JsonDeSerialize<GetUser>(GetUserAccount);
if (listUser != null)
{
//有账号,判断该账号是否是管理员
UserAccount = listUser.UserId;
Log.CreateLogManager().Debug("UserAccount=" + UserAccount);

try
{
if (!string.IsNullOrEmpty(UserAccount))
{
Log.CreateLogManager().Debug(UserAccount);
string GetAdministratorAPIURL = FK.SN.Utilities.WebPage.ConfigHelper.GetConfigStr("GetAdministrator");
Log.CreateLogManager().Debug("GetAdministratorAPIURL=" + GetAdministratorAPIURL);
// Log.CreateLogManager().Debug("获取到了连接字符串GetSalesRepAPI:" + GetSalesDataAPIURL);
IDictionary<string, string> parametersAdmin = new Dictionary<string, string>();
parametersAdmin.Add("UserAccount", UserAccount);
// Log.CreateLogManager().Debug("获取到了参数个数:" + parameters.Count);
string Data = HttpUtils.DoGet(GetAdministratorAPIURL, parametersAdmin);

Log.CreateLogManager().Debug("获取到了Data:" + Data);
if (!string.IsNullOrEmpty(Data))
{
Log.CreateLogManager().Debug("Data=" + Data);
context.Response.Write(Data);
}
else
{
context.Response.Write("false");
}
}

}
catch (Exception ex)
{
context.Response.Write("false");
}

}
}
else
{
Log.CreateLogManager().Debug("未获取到access_token");
}


}

public bool IsReusable
{
get
{
return false;
}
}
}
class GetUser
{
public string UserId { get; set; }
public string DeviceId { get; set; }
public int errcode { get; set; }
public string errmsg { get; set; }
}

class GetToken
{
public int errcode { get; set; }
public string errmsg { get; set; }
public string access_token { get; set; }
public int expires_in { get; set; }
}
}

C# 开发微信扫码登录

标签:open   反序   linq   err   post   管理   use   doc   连接   

原文地址:http://www.cnblogs.com/sensenwang/p/7516352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!