微信跳转网页读取openid的方法(asp.net)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liheao/article/details/77249206

1.做的准备工作:看公众平台文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

2.在微信公众号请求用户网页授权之前,开发者需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名例如:www.qq.com,你必须配置成www.123.com

3.研究这个链接地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect 这是一个微信的地址。

如果你的地址是https://www.123.com/abc.aspx

那么请把https://www.123.com/abc.aspx这个转换成使用urlEncode对链接进行处理,工具地址http://tool.chinaz.com/tools/urlencode.aspx

生成这个地址   https%3a%2f%2fwww.123.com%2fabc.aspx

你肯定有一个公众号的appid=QWERTY23456

那么你把地址改成这个

https://open.weixin.qq.com/connect/oauth2/authorize?appid=QWERTY23456&redirect_uri=https%3a%2f%2fwww.123.com%2fabc.aspx&response_type=code&scope=snsapi_base&state=123#wechat_redirect

这个就是你的链接地址,它给你一个code,你通过网页获取,就是你的https://www.123.com/abc.aspx获取。

4.做sap.NET中C#的开发,abc.aspx代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.IO;
using System.Text;
using System.Net;

public partial class index002 : System.Web.UI.Page

{

    public string xxx;
    protected void Page_Load(object sender, EventArgs e)
    {
        string code = Request["code"].ToString();
        Response.Write("CODE: ");
        Response.Write(code);
        Response.Write("<br/>");
        string html = string.Empty;
        string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=QWERTY23456&secret=你的密码&code=" + code + "&grant_type=authorization_code";
         HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "GET";
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        Stream ioStream = response.GetResponseStream();
        StreamReader sr = new StreamReader(ioStream, Encoding.UTF8);
        html = sr.ReadToEnd();
        sr.Close();
        ioStream.Close();
        response.Close();
        string key = "\"openid\":\"";
        int startIndex = html.IndexOf(key);
        if (startIndex != -1)
        {
            int endIndex = html.IndexOf("\",", startIndex);
            string openid = html.Substring(startIndex + key.Length, endIndex - startIndex - key.Length);
      
            Response.Write("OPENID: ");
            Response.Write(openid);
           

        }
        else
        {
            Response.Write("获取openid未成功!");
        }
 

    }
}

5.ok
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值