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; }
}
}