首页 > 其他分享 >微信客户端登陆

微信客户端登陆

时间:2023-04-25 18:34:30浏览次数:38  
标签:return string 微信 private access token 登陆 public 客户端

界面:

<Window x:Class="WpfApp1.weixin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="weixin" Height="450" Width="800">
    <Grid>
        <WebBrowser x:Name="weixinbrowser" ScrollViewer.VerticalScrollBarVisibility ="Disabled"  UseLayoutRounding="False" Grid.ColumnSpan="3" Navigating="WeChatWebBrowser_Navigating" LoadCompleted="WeChatWebBrowser_LoadCompleted" Loaded="WeChatWebBrowser_Loaded" Navigated="WeChatWebBrowser_Navigated"/>
    </Grid>
</Window>

 

后台:

namespace WpfApp1
{
    /// <summary>
    /// weixin.xaml 的交互逻辑
    /// </summary>
    public partial class weixin : Window
    {
        public weixin()
        {
            InitializeComponent();
            weixinbrowser.Navigate(new Uri("xxx"));
        }

        #region 属性
        public string appid = "xxx";  //公众微信平台下可以找到
        public string appsecret = "xxx";  //公众微信平台下可以找到

        #endregion

        //根据appid,secret,code获取微信openid、access token信息
        protected OAuth_Token Get_token(string Code)
        {
            //获取微信回传的openid、access token
            string Str = GetJson("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + Code + "&grant_type=authorization_code");
            //微信回传的数据为Json格式,将Json格式转化成对象
            OAuth_Token Oauth_Token_Model = JSONHelper.ParseFromJson<OAuth_Token>(Str);
            return Oauth_Token_Model;
        }

        //刷新Token(好像这个刷新Token没有实际作用)
        protected OAuth_Token refresh_token(string REFRESH_TOKEN)
        {
            string Str = GetJson("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + appid + "&grant_type=refresh_token&refresh_token=" + REFRESH_TOKEN);
            OAuth_Token Oauth_Token_Model = JSONHelper.ParseFromJson<OAuth_Token>(Str);
            return Oauth_Token_Model;
        }

        //根据openid,access token获得用户信息
        protected OAuthUser Get_UserInfo(string REFRESH_TOKEN, string OPENID)
        {
            string Str = GetJson("https://api.weixin.qq.com/sns/userinfo?access_token=" + REFRESH_TOKEN + "&openid=" + OPENID);
            OAuthUser OAuthUser_Model = JSONHelper.ParseFromJson<OAuthUser>(Str);
            return OAuthUser_Model;
        }

        //访问微信url并返回微信信息
        protected string GetJson(string url)
        {
            WebClient wc = new WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;
            wc.Encoding = Encoding.UTF8;
            string returnText = wc.DownloadString(url);

            if (returnText.Contains("errcode"))
            {
                //可能发生错误
            }
            return returnText;
        }

        private void WeChatWebBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            SetWebBrowserSilent(sender as WebBrowser, true);
            string url = e.Uri.ToString();
            if (url.Contains("code"))
            {
                string code = "";
                int iStart = url.IndexOf("=");
                int iEnd = url.IndexOf('&', iStart);
                if (iEnd < 0)
                {
                    iEnd = url.Length - iStart;
                }
                else
                {
                    iEnd -= iStart;
                }
                code = url.Substring(iStart + 1, iEnd - 1);

                OAuth_Token model = Get_token(code);  //获取access_token
                if (string.IsNullOrEmpty(model.access_token) || string.IsNullOrEmpty(model.openid))
                {
                    return;
                }

                OAuthUser OAuthUser_Model = Get_UserInfo(model.access_token, model.openid);//获取用户信息
                return;
            }

            //不提示脚本错误
            SetWebBrowserSilent(sender as WebBrowser, true);

        }
        private void WeChatWebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            HTMLDocument dom = (mshtml.HTMLDocument)weixinbrowser.Document; //定义HTML
            dom.documentElement.style.overflow = "hidden";    //隐藏浏览器的滚动条
            dom.body.setAttribute("scroll", "no");            //禁用浏览器的滚动条
            SetWebBrowserSilent(sender as WebBrowser, true);
        }

        private void WeChatWebBrowser_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void WeChatWebBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
        }
        /// <summary>
        /// 设置浏览器静默,不弹错误提示框
        /// </summary>
        /// <param name="webBrowser">要设置的WebBrowser控件浏览器</param>
        /// <param name="silent">是否静默</param>
        private void SetWebBrowserSilent(WebBrowser webBrowser, bool silent)
        {
            FieldInfo fi = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
            if (fi != null)
            {
                object browser = fi.GetValue(webBrowser);
                if (browser != null)
                    browser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, browser, new object[] { silent });
            }
        }

        /// <summary>
        /// TitleBar_MouseDown - Drag if single-click, resize if double-click
        /// </summary>
        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                if (e.ClickCount == 2)
                {
                    //AdjustWindowSize();
                }
                else
                {
                    this.DragMove();
                }
        }
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }


    /// <summary>
    /// 微信Access_token类
    /// </summary>
    public class OAuth_Token
    {
        //access_token    网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
        //expires_in    access_token接口调用凭证超时时间,单位(秒)
        //refresh_token    用户刷新access_token
        //openid    用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
        //scope    用户授权的作用域,使用逗号(,)分隔
        public string _access_token;
        public string _expires_in;
        public string _refresh_token;
        public string _openid;
        public string _scope;
        public string access_token
        {
            set { _access_token = value; }
            get { return _access_token; }
        }
        public string expires_in
        {
            set { _expires_in = value; }
            get { return _expires_in; }
        }

        public string refresh_token
        {
            set { _refresh_token = value; }
            get { return _refresh_token; }
        }
        public string openid
        {
            set { _openid = value; }
            get { return _openid; }
        }
        public string scope
        {
            set { _scope = value; }
            get { return _scope; }
        }

    }

    /// <summary>
    /// 微信用户信息类
    /// </summary>
    public class OAuthUser
    {
        public OAuthUser()
        { }
        #region 数据库字段
        private string _openID;
        private string _searchText;
        private string _nickname;
        private string _sex;
        private string _province;
        private string _city;
        private string _country;
        private string _headimgUrl;
        private string _privilege;
        private string _unionid;

        #endregion

        #region 字段属性
        /// <summary>
        /// 用户的唯一标识
        /// </summary>
        public string openid
        {
            set { _openID = value; }
            get { return _openID; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string SearchText
        {
            set { _searchText = value; }
            get { return _searchText; }
        }
        /// <summary>
        /// 用户昵称 
        /// </summary>
        public string nickname
        {
            set { _nickname = value; }
            get { return _nickname; }
        }
        /// <summary>
        /// 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知 
        /// </summary>
        public string sex
        {
            set { _sex = value; }
            get { return _sex; }
        }
        /// <summary>
        /// 用户个人资料填写的省份
        /// </summary>
        public string province
        {
            set { _province = value; }
            get { return _province; }
        }
        /// <summary>
        /// 普通用户个人资料填写的城市 
        /// </summary>
        public string city
        {
            set { _city = value; }
            get { return _city; }
        }
        /// <summary>
        /// 国家,如中国为CN 
        /// </summary>
        public string country
        {
            set { _country = value; }
            get { return _country; }
        }
        /// <summary>
        /// 用户头像 
        /// </summary>
        public string headimgurl
        {
            set { _headimgUrl = value; }
            get { return _headimgUrl; }
        }
        /// <summary>
        /// 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)其实这个格式称不上JSON,只是个单纯数组
        /// </summary>
        public string privilege
        {
            set { _privilege = value; }
            get { return _privilege; }
        }
        public string unionid
        {
            set { _unionid = value; }
            get { return _unionid; }
        }
        #endregion
    }
}

 

标签:return,string,微信,private,access,token,登陆,public,客户端
From: https://www.cnblogs.com/anjingdian/p/17353511.html

相关文章

  • Citrix XenDesktop 2203实现只允许图片从客户端拷贝至云桌面
    一、前言最近遇到个别客户需求,要求只允许从本地拷贝图片至云桌面,禁止其他文件、文字拷贝,禁止从VDA拷贝任何数据到客户端本地。二、实现步骤1、只允许图片从客户端拷贝到云桌面会话剪贴板写入允许的格式---需要设置CF_BITMAP(图片)客户端剪贴板重定向------需要开启限制会话剪贴板......
  • 微博客户端登陆
    界面:<Windowx:Class="WpfApp1.weibo"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.c......
  • 微信小程序 swiper scroll 应用
    swiper演示视图wxml<!--中部上游信息--><swiperclass="swiper"autoplaycircularprevious-margin="20rpx"next-margin="60rpx"><swiper-itemwx:for="{{upstreamList}}"wx:key="id"><......
  • ShareSDK 微信平台注册指南
    注册微信平台账号登录微信开放平台网址微信开放平台网址: https://open.weixin.qq.com/开发者认证1.进入账号中心,进行开发者企业认证2.认证成功后如截图所示注意:开发者企业认证和安卓端需要提交的软著公司需一致,否则需提供软著授权协议证明创建应用填写基本信息按照要求填入填入......
  • 手写签名-微信小程序
    index.wxml<canvastype="2d"id="canvas"bindtouchmove="move"bindtouchstart="start"binderror="error"style="width:{{width}}px;height:{{height}}px;"></canvas><viewclass="b......
  • 微信公众号使用隐藏页判断登录
    <scripttype="text/javascript">   $(document).ready(function(){      document.getElementById("over").style.display="block";      document.getElementById("layout").style.display="block";      //判断......
  • 解决企业微信在线文档里的代码复制后不能执行的问题
    【问题】某日帮同事改了个SQL,自己能执行,因为懒得另存为,就直接丢到企业微信发了。企业微信提示因为超长生成了在线文档。同事复制后,执行一直报错,类似下面的情况:【报错】 【能执行】【原因】既然语法正确,那么就把文档下载下来瞅瞅。以下是WPS的检查方法,word检查方法类似。......
  • C/C++服务端客户端通讯程序[2023-04-24]
    C/C++服务端客户端通讯程序[2023-04-24]Socket通讯程序..服务器端).pptx任务:Socket通讯程序开发·基本要求(80分)∶完成一对一的Socket客户端与服务器程序·进阶要求(90分)∶在完成基本要求基础上,将服务器端程序改为多线程程序·高级要求(100分)︰将客户端和服务器端都改为多......
  • 微信小程序 + App = ?
    为了让开发者可以一次编码,就能够编译为 小程序、Android、iOS 应用,实现所谓的 多端开发,去年微信推出了Donut(多纳)开发平台(目前是Beta版),核心包括:1、开发者可将小程序构建成可独立运行的移动应用;2、也可以将小程序构建成运行于原生应用中的业务模块从图中可以看出......
  • 如何解决安卓七,客户端用的webview打包视频切换有暂停图标
    可以用video中的html5poster属性poster="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7为最小的base64图片并且透明也可以选择黑色:data:image/gif;b......