界面:
<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