首页 新闻 会员 周边 捐助

谁有PC端微信扫码登录demo 啊

0
悬赏园豆:50 [待解决问题]

,原理懂一点,但是还不会写啊,看了好多,都是php的,求助

寨♂冇嚸ル壞的主页 寨♂冇嚸ル壞 | 初学一级 | 园豆:186
提问于:2016-12-07 16:50
< >
分享
所有回答(3)
1

基本的代码如下,自己参考一下。项目里面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; }
        }
    }

 

czd890 | 园豆:14488 (专家六级) | 2016-12-08 10:27

那个二维码怎么生成啊

支持(1) 反对(0) 寨♂冇嚸ル壞 | 园豆:186 (初学一级) | 2016-12-08 10:41

@寨♂冇嚸ル壞:

微信网页登录里面的二维码是微信生成的

http://mp.weixin.qq.com/wiki/4/9ac2e7b1f1d22e9e57260f6553822520.html

支持(1) 反对(0) czd890 | 园豆:14488 (专家六级) | 2016-12-08 10:52
0

多谢calvinK,代码很有用

东篱南山 | 园豆:208 (菜鸟二级) | 2017-04-21 13:27
0

请问一下我的微信二维码已经形成,但是下一步要做手机上登录授权

dingmengshi | 园豆:202 (菜鸟二级) | 2018-04-04 15:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册