,原理懂一点,但是还不会写啊,看了好多,都是php的,求助
基本的代码如下,自己参考一下。项目里面copy过来的
public class WXAPIController : Controller { string appid = "..."; string secret = "..."; //前台登录扫码按钮 public ActionResult Req(string ReturnUrl) { 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<ActionResult> Login(string code, string state, string ReturnUrl) { 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<GetAccessToken>(); 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<UserInfo>(); 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"); } class GetAccessToken { 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; } static GetAccessToken() { Mapper.CreateMap<GetAccessToken, User_WX>() .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)); } } class UserInfo { 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; } } }
那个二维码怎么生成啊
@寨♂冇嚸ル壞:
微信网页登录里面的二维码是微信生成的
http://mp.weixin.qq.com/wiki/4/9ac2e7b1f1d22e9e57260f6553822520.html
请问一下我的微信二维码已经形成,但是下一步要做手机上登录授权