基本信息
源码名称:微信授权登录手机asp.net
源码大小:18.03M
文件格式:.zip
开发语言:C#
更新时间:2016-11-24
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
需把以下信息修改为自己申请的公众号的信息( 代码在WeiXinTest项目中BasePage.cs中) 这行代码
//获取appId,appSecret的配置信息
string appId = "wx12345678****";
string appSecret = "25f9e794323b453885f****";
string redirecturl = HttpContext.Current.Request.Url.ToString();//这里需要改成 你在微信授权的 url网址,否则会提示 redirect_uri 参数错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WeiXinOAuth;
using WeiXinOAuth.Moder;
namespace WeiXinTest
{
public partial class BasePage : System.Web.UI.Page
{
public BasePage()
{
this.Page.Load = new EventHandler(Page_Load);
this.Page.Unload = new EventHandler(Page_UnLoad);
}
protected void Page_Load(object sender, EventArgs e)
{
DoWith();
}
protected void Page_UnLoad(object sender, EventArgs e)
{
}
private void DoWith()
{
//用户尚未登录
if (true)
{
//获取appId,appSecret的配置信息
string appId = "wx123456****";
string appSecret = "25f9e794323b453885f5*****";
string redirecturl = HttpContext.Current.Request.Url.ToString();//这里需要改成 你在微信授权的 url网址,否则会提示 redirect_uri 参数错误
OAuth weixinOAuth = new OAuth();
//微信第一次握手后得到的code 和state
string _code = Request.QueryString["code"];
string _state = Request.QueryString["state"];
if (_code == "" || string.IsNullOrEmpty(_code) || _code == "authdeny" || string.IsNullOrEmpty(_state))
{
if (_code == "" || string.IsNullOrEmpty(_code))
{
//发起授权(第一次微信握手)
string _authUrl = weixinOAuth.GetWeiXinCode(appId, appSecret, HttpContext.Current.Server.UrlEncode(redirecturl));
HttpContext.Current.Response.Redirect(_authUrl, true);
}
else
{ // 用户取消授权
HttpContext.Current.Response.Redirect("~/Error.html", true);
}
}
else
{
//获取微信的Access_Token(第二次微信握手)
WeiXinAccessTokenResult modelResult = weixinOAuth.GetWeiXinAccessToken(appId, appSecret, _code);
//获取微信的用户信息(第三次微信握手)
WeiXinUserInfoResult _userInfo = weixinOAuth.GetWeiXinUserInfo(modelResult.SuccessResult.access_token, modelResult.SuccessResult.openid);
//用户信息(判断是否已经获取到用户的微信用户信息)
if (_userInfo.Result && _userInfo.UserInfo.openid != "")
{
//保存获取到的用户微信用户信息,并保存到数据库中
Response.Write("openid:" _userInfo.UserInfo.openid ";用户昵称:" _userInfo.UserInfo.nickname);
}
else
{
Response.Write("获取用户OpenId失败");
}
}
}
}
}
}