php pc端微信扫码登录失败,谁有PC端微信扫码登录demo 啊

public classWXAPIController : Controller

{string appid = "...";string secret = "...";//前台登录扫码按钮

public ActionResult Req(stringReturnUrl)

{var rUrl = this.Server.UrlEncode("https://***/WXAPI/Login?ReturnUrl=" +ReturnUrl);var state = Guid.NewGuid().ToString("N").ToLower();this.Session["wx_state"] =state;var reqUrl = "https://" + "open.weixin.qq.com/connect/qrconnect?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_login&state={2}#wechat_redirect".FormatEx(appid, rUrl, state);return this.Redirect(reqUrl);

}//扫码完成,微信回调action

public async Task Login(string code, string state, stringReturnUrl)

{if (!state.Equals(this.Session["wx_state"]))return this.Content("he state does not match. You may be a victim of CSRF.");if(code.IsNullOrEmpty())return this.Content("your not auth");var reqUrl = "https://" + "api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code".FormatEx(appid, secret, code);var resultStr = await HttpRequestTools.Instance.DoRequestAsync(reqUrl, method: HttpRequestTools.HttpMethod.GET, timeout: 10000);

ServiceFocus.LogService.AddLog("微信联合登陆,请求accesstoken", resultStr);var json = resultStr.ToJson();if(json.errcode.IsNotNullOrEmpty())return this.Content("wei xin server is error");var accessToken =json.access_token;

{

reqUrl= "https://" + "api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh-CN".FormatEx(json.access_token, json.openid);/**

{

"openid":"OPENID",

"nickname":"NICKNAME",

"sex":1,

"province":"PROVINCE",

"city":"CITY",

"country":"COUNTRY",

"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",

"privilege":[

"PRIVILEGE1",

"PRIVILEGE2"

],

"unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"

}*/resultStr= await HttpRequestTools.Instance.DoRequestAsync(reqUrl, method: HttpRequestTools.HttpMethod.GET, timeout: 10000);

ServiceFocus.LogService.AddLog("微信联合登陆,获取用户基本信息", resultStr);var json2 = resultStr.ToJson();

user_wx.nickname=json2.nickname;

user_wx.sex=json2.sex;

user_wx.province=json2.province;

user_wx.city=json2.city;

user_wx.country=json2.country;

user_wx.headimgurl=json2.headimgurl;

user_wx.IsGetUseInfo= true;

}

AccountModel.LoginIn(user);if(ReturnUrl.IsNotNullOrEmpty())return this.Redirect(ReturnUrl);return this.RedirectToAction("Index", "Home");

}classGetAccessToken

{public string errcode { get; set; }public string errmsg { get; set; }public string access_token { get; set; }public string expires_in { get; set; }public string refresh_token { get; set; }public string openid { get; set; }public string scope { get; set; }public string unionid { get; set; }staticGetAccessToken()

{

Mapper.CreateMap()

.ForMember(p=> p.access_token, x => x.MapFrom(z =>z.access_token))

.ForMember(p=> p.expires_in, x => x.MapFrom(z =>z.expires_in))

.ForMember(p=> p.refresh_token, x => x.MapFrom(z =>z.refresh_token))

.ForMember(p=> p.WXOpenId, x => x.MapFrom(z =>z.openid))//.ForMember(p => p.sco, x => x.MapFrom(z => z.scope))

.ForMember(p => p.WXUnionId, x => x.MapFrom(z =>z.unionid));

}

}classUserInfo

{public string nickname { get; set; }public int sex { get; set; }public string province { get; set; }public string city { get; set; }public string country { get; set; }public string headimgurl { get; set; }

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值