用.Net开发微信的时候第一步就是获取微信的网页授权,获取openid。

自己做个总结,以后也好用,这里只提供了获取openid的接口,后续程序有待开发

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Web.Script.Serialization; namespace WechatHelper
{
/// <summary>
/// 在网站下创建WeChat文件夹,将微信页面放在WeChat文件夹下
/// 调用GetOpenID()方法。
/// </summary>
public class WeChatHelper
{
public static string appId = "在微信公共平台中拿";
public static string secret = "在微信公共平台中拿";
public static string code = ""; /// <summary>
/// 获取openID
/// </summary>
/// <param name="type">回调页面的绝对路径</param>
public static void GetOpenID(string type)
{
if ((HttpContext.Current.Request.Cookies["openid"]) == null)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
Dictionary<string, object> dict = new Dictionary<string, object>();
string code = GetCode(type); //获取code
AccessToken(code); //获取accessToken
HttpContext.Current.Response.Redirect("/WeChat/" + type + ".aspx");
}
}
/// <summary>
/// 获取code代码
/// </summary>
/// <returns></returns>
public static string GetCode(string TypeName)
{
if (HttpContext.Current.Request.QueryString["Code"] != null) //判断code是否存在
{
if (HttpContext.Current.Request.Cookies["Code"] == null) //判断是否是第二次进入
{
SetCookie("code", HttpContext.Current.Request.QueryString["Code"], ); //写code 保存到cookies
code = HttpContext.Current.Request.QueryString["Code"];
}
else
{
delCookies("code"); //删除cookies CodeURL(TypeName); //code重新跳转URL
}
}
else
{
CodeURL(TypeName); //code跳转URL
}
return code;
}
/// <summary>
/// 获取AccessToken
/// </summary>
/// <returns></returns>
public static string AccessToken(string code)
{
Dictionary<string, string> obj = new Dictionary<string, string>();
var client = new System.Net.WebClient();
var serializer = new JavaScriptSerializer();
string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appId, secret, code);
client.Encoding = System.Text.Encoding.UTF8;
string dataaccess = "";
try
{
dataaccess = client.DownloadString(url);
}
catch (Exception e)
{
//存log方法
}
//获取字典
obj = serializer.Deserialize<Dictionary<string, string>>(dataaccess);
string accessToken = "";
if (obj.TryGetValue("access_token", out accessToken)) //判断access_Token是否存在
{
SetCookie("openid", obj["openid"], );
}
else //access_Token 失效时重新发送。
{
//存log方法
}
return accessToken;
}
/// <summary>
/// 设置cookies
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="time"></param>
public static void SetCookie(string name, string value, int time)
{
HttpCookie cookies = new HttpCookie(name);
cookies.Name = name;
cookies.Value = value;
cookies.Expires = DateTime.Now.AddDays(time);
HttpContext.Current.Response.Cookies.Add(cookies); }
/// <summary>
/// 跳转codeURL
/// </summary>
/// <param name="TypeName"></param>
public static void CodeURL(string TypeName)
{
string url = "";
string locationhref = "http://网站域名/WeChat/"+TypeName+".aspx";
url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect", appId, locationhref);
HttpContext.Current.Response.Redirect(url);
}
/// <summary>
/// 删除cookies
/// </summary>
/// <param name="name"></param>
public static void delCookies(string name)
{
foreach (string cookiename in HttpContext.Current.Request.Cookies.AllKeys)
{
HttpCookie cookies = HttpContext.Current.Request.Cookies[name];
if (cookies != null)
{
cookies.Expires = DateTime.Today.AddDays(-);
HttpContext.Current.Response.Cookies.Add(cookies);
HttpContext.Current.Request.Cookies.Remove(name);
}
}
}
}
}

微信(一) 获取openid 网页授权 C# WeChatHelper的更多相关文章

  1. 亲历H5移动端游戏微信支付接入及那些坑(二)——获取Openid和授权

    第一篇中将一些坑说明,那么这篇开始正式进入接入步骤.具体的参数说明,我不会列出,毕竟微信官方文档都有,我想大家都看的懂,而且这接口也有可能微信会变动,所以不列出来,也是不想引起大家的误解,接入步骤只起 ...

  2. C#微信公众号开发--网页授权(oauth2.0)获取用户基本信息二

    前言 这一篇实现snsapi_userinfo,写这篇时其实我是有疑惑的,因为我并没有调试成功,但是我反复检查程序和思路是没有问题的,因为我使用的测试公众号,群里一个伙计说他之前调试时用的也是测试公众 ...

  3. C#微信公众号开发--网页授权(oauth2.0)获取用户基本信息一

    前言 微信网页授权共分为两种方式:snsapi_base.snsapi_userinfo. snsapi_base需要关注公众号,获取用户信息时不弹出用户授权界面. snsapi_userinfo是在 ...

  4. ASP.NET MVC5+EF6+EasyUI 后台管理系统(76)-微信公众平台开发-网页授权

    前言 网页授权是:应用或者网站请求你用你的微信帐号登录,同意之后第三方应用可以获取你的个人信息 网上说了一大堆参数,实际很难理解和猜透,我们以实际的代码来演示比较通俗易懂 配置 实现之前我们必须配置用 ...

  5. 微信开发之Author网页授权

     微信开发中,经常有这样的需求:获得用户头像.绑定微信号给用户发信息.. 那么实现这些的前提就是授权!   1.配置安全回调域名: 在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的&q ...

  6. 绑定微信以及获取openId

    由于公司最近在做一个微信公众号的项目,需要获取用户openId,我再一次踏入了微信的坑! 先在这里告诫后来的同志,如果一样要开始做有关微信的东西,最好是有前辈,或者直接看完文档,不懂或者纳闷的地方直接 ...

  7. [转] Android进阶——安卓接入微信,获取OpenID

    PS: sendAuthRequest拿到code,通过code拿到access_token和openId,access_token可以拿到用户的信息 http://blog.csdn.net/hao ...

  8. asp.net mvc 根据浏览器判断,如果是微信浏览器则进行网页授权,否则直接访问

    遇到这个需求,想到的第一点就是,这个肯定是需要写在一个通用的地方.方便调用.一般可以定义个 父类控制器在OnActionExcuting方法执行前写逻辑,先上代码,一边写代码一边讲解: /// < ...

  9. 微信企业号获取OpenID过程

    define('CorpID', "wx82e2c31215d9a5a7"); define('CorpSecret', ""); //当前管理组 设置-> ...

随机推荐

  1. c语言write与python的struct模块交互

    以下讲的都是用二进制形式打开文件.网上有很多struct模块的文章,下面是我做的小实验. 1.对于c里面的fwrite写入一个单字节,写的就是它的二进制.如3,写入文件就是二进制0x03,它并不是3的 ...

  2. NGINX源代码剖析 之 CPU绑定(CPU亲和性)

    作者:邹祁峰 邮箱:Qifeng.zou.job@gmail.com 博客:http://blog.csdn.net/qifengzou 日期:2014.06.12 18:44 转载请注明来自&quo ...

  3. 利用FluorineFx的ByteArray上传图片

    Flex端利用new PNGEncoder().encode(bitmapData)将png图片转换为ByteArray,然后传给服务器,服务端需要定义一个public ByteArray Uploa ...

  4. innodb_lru_scan_depth

    innodb_lru_scan_depth是5.6新增加的参数,根据 官方文档 描述,它会影响page cleaner线程每次刷脏页的数量, 这是一个每1秒  loop一次的线程.在Innodb内部, ...

  5. 龙书(Dragon book) +鲸书(Whale book)+虎书(Tiger book)

    1.龙书(Dragon book)书名是Compilers: Principles,Techniques,and Tools作者是:Alfred V.Aho,Ravi Sethi,Jeffrey D. ...

  6. IPC——数据报套接字通信

    Linux进程间通信——使用数据报套接字 前一篇文章,Linux进程间通信——使用流套接字介绍了一些有关socket(套接字)的一些基本内容,并讲解了流套接字的使用,这篇文章将会给大家讲讲,数据报套接 ...

  7. RAID设备

    RAID(Redundant Array of Independent Disk),独立冗余磁盘阵列(通常简称磁盘阵列).磁盘阵列的实质是将多个磁盘通过RAID控制器组合在一起,形成一个新的磁盘.这个 ...

  8. 重新组织 vs 重新生成索引

    索引是数据库引擎中针对表(有时候也针对视图)建立的特别数据结构,用来帮助查找和整理数据.索引的重要性体现在能够使数据库引擎快速返回查询 结果.当对索引所在的基础数据表进行修改时(包括插入.删除和更新等 ...

  9. nexus-2.13.0-01.war

    https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.13.0-01.war

  10. react-redux 学习笔记

    react 是 view 层的一个框架,负责展示数据:redux 控制数据流动,把数据存在唯一的 store 里,通过 action 来触发事件,reducer 来根据事件处理数据. redux 在通 ...