最近研究微信公众平台,这里整理了一下向特定用户推送消息的思路
一、首先需要将微信的openid与系统用户绑定。
在用户关注公众平台的时候,回复一个链接,要求用户绑定,可以设计如下消息进行回复,(openid最好进行加密处理,后者还需要用这个字段绑定fakeid)。
欢迎关注有问必答平台,点击此处进行用户绑定!
在bind.html页面将openid与系统的usercode进行绑定,这个绑定过程非常简单,这里不详叙述。
二、将openid与fakeid进行绑定
微信公众平台是一回一答的模式;但是在微信公众平台后台,可以向特定用户进行消息发送。我们利用这个机制使用代码去模拟这个过程来实现消息推送。
首先需要模拟登录:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
///
///WeiXinLogin 的摘要说明
///
public class WeiXinLogin
{
///
/// MD5 32位加密
///
///
///
static string GetMd5Str32(string str)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
char[] temp = str.ToCharArray();
byte[] buf = new byte[temp.Length];
for (int i = 0; i < temp.Length; i++)
{
buf[i] = (byte)temp[i];
}
byte[] data = md5Hasher.ComputeHash(buf);
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
public static bool ExecLogin(string name,string pass)
{
bool result = false;
string password = GetMd5Str32(pass).ToUpper();
string padata = "username=" + name + "&pwd=" + password + "&a