/// <summary> /// 回调 /// </summary> public ActionResult CallBack() { //返回url string returnUrl = WebHelper.GetQueryString("returnUrl"); if (returnUrl.Length == 0) { returnUrl = "/"; } if (WorkContext.MallConfig.LoginType == "") { return(PromptView(returnUrl, "商城目前已经关闭登陆功能!")); } if (WorkContext.Uid > 0) { return(PromptView(returnUrl, "您已经登录,无须重复登录!")); } //返回的随机值 string backSalt = WebHelper.GetQueryString("state"); //Authorization Code string code = WebHelper.GetQueryString("code"); //保存在session中随机值 string salt = Sessions.GetValueString(WorkContext.Sid, "qqAuthLoginSalt"); if (backSalt.Length > 0 && code.Length > 0 && salt.Length > 0 && backSalt == salt) { //清空session中随机值 Sessions.SetItem(WorkContext.Sid, "qqAuthLoginSalt", null); PluginSetInfo pluginSetInfo = PluginUtils.GetPluginSet(); //构建获取Access Token的参数 string postData = string.Format("grant_type=authorization_code&code={0}&client_id={1}&client_secret={2}&redirect_uri=http://{3}{4}", code, pluginSetInfo.AppKey, pluginSetInfo.AppSecret, BMAConfig.MallConfig.SiteUrl, Url.Action("CallBack")); //发送获得Access Token的请求 string result = WebHelper.GetRequestData(pluginSetInfo.AuthUrl + "/oauth2.0/token", postData); //将返回结果解析成参数列表 NameValueCollection parmList = WebHelper.GetParmList(result); //Access Token值 string access_token = parmList["access_token"]; //通过上一步获取的Access Token,构建获得对应用户身份的OpenID的url string url = string.Format("{0}/oauth2.0/me?access_token={1}", pluginSetInfo.AuthUrl, access_token); //发送获得OpenID的请求 result = WebHelper.GetRequestData(url, "get", null); //移除返回结果开头的“callback(”和结尾的“);”字符串 string json = StringHelper.TrimEnd(StringHelper.TrimStart(result, "callback("), ");"); //OpenID值 string openId = JSON.ToObject <PartOAuthUser>(json).OpenId; //判断此用户是否已经存在 int uid = OAuths.GetUidByOpenIdAndServer(openId, pluginSetInfo.Server); if (uid > 0)//存在时 { PartUserInfo partUserInfo = Users.GetPartUserById(uid); //更新用户最后访问 Users.UpdateUserLastVisit(partUserInfo.Uid, DateTime.Now, WorkContext.IP, WorkContext.RegionId); //更新购物车中用户id Carts.UpdateCartUidBySid(partUserInfo.Uid, WorkContext.Sid); MallUtils.SetUserCookie(partUserInfo, -1); return(Redirect("/")); } else { //获取用户信息的url url = string.Format("{0}/user/get_user_info?access_token={1}&oauth_consumer_key={2}&openid={3}", pluginSetInfo.AuthUrl, access_token, pluginSetInfo.AppKey, openId); //发送获取用户信息的请求 result = WebHelper.GetRequestData(url, "get", null); //将返回结果序列化为对象 OAuthUser oAuthUser = JSON.ToObject <OAuthUser>(result); if (oAuthUser.Ret == 0)//当没有错误时 { UserInfo userInfo = OAuths.CreateOAuthUser(oAuthUser.Nickname, pluginSetInfo.UNamePrefix, openId, pluginSetInfo.Server, WorkContext.RegionId); if (userInfo != null) { //发放注册积分 Credits.SendRegisterCredits(ref userInfo, DateTime.Now); //更新购物车中用户id Carts.UpdateCartUidBySid(userInfo.Uid, WorkContext.Sid); MallUtils.SetUserCookie(userInfo, -1); return(Redirect("/")); } else { return(PartialView("用户创建失败")); } } else { return(PartialView("QQ授权登陆失败")); } } } else { return(Redirect("/")); } }
/// <summary> /// 回调 /// </summary> public ActionResult CallBack() { //返回url string returnUrl = WebHelper.GetQueryString("returnUrl"); if (returnUrl.Length == 0) { returnUrl = "/"; } string[] rtval = Request.QueryString.AllKeys; if (Array.IndexOf <string>(rtval, "code") == -1) { return(PartialView("您未授权微信登录,请重新扫描登陆")); } if (WorkContext.ShopConfig.LoginType == "") { return(PromptView(returnUrl, "目前已经关闭登陆功能!")); } if (WorkContext.Uid > 0) { return(PromptView(returnUrl, "您已经登录,无须重复登录!")); } //返回的随机值 string backSalt = WebHelper.GetQueryString("state"); //Authorization Code string code = WebHelper.GetQueryString("code"); //获取access_token string tokenurl = WeiXinOAuth.GetWeiXinRqUrl(pluginSetInfo.AppID, pluginSetInfo.AppSecret, "", code, "", "", "access_token"); WeiXinAccessTokenResult token = WeiXinOAuth.GetAccessToken(tokenurl, pluginSetInfo.AppID, pluginSetInfo.AppSecret); if (token.ErrorResult.errcode == 40029) { return(PartialView("获取微信授权码错误,请重新扫描登陆")); } //判断此用户是否已经存在 int uid = OAuths.GetUidByOpenIdAndServer(token.SuccessResult.openid, pluginSetInfo.Server); if (uid > 0)//存在时 { PartUserInfo partUserInfo = Users.GetPartUserById(uid); //更新用户最后访问 Users.UpdateUserLastVisit(partUserInfo.Uid, DateTime.Now, WorkContext.IP, WorkContext.RegionId); ShopUtils.SetUserCookie(partUserInfo, -1, "web"); return(Redirect("/")); } else { //获取用户信息 string userurl = WeiXinOAuth.GetWeiXinRqUrl("", "", "", "", token.SuccessResult.openid, token.SuccessResult.access_token, "openid"); WeiXinUserInfoResult userinfo = WeiXinOAuth.GetWeiXinUserInfo(userurl); if (userinfo.ErrorMsg.errcode == 40003) { return(PartialView("获取用户信息失败,请重新扫描登陆")); } UserInfo userInfo = OAuths.CreateOAuthUser(userinfo.UserInfo.nickname, pluginSetInfo.UNamePrefix, token.SuccessResult.openid, pluginSetInfo.Server, WorkContext.RegionId, userinfo.UserInfo.unionid); if (userInfo != null) { ShopUtils.SetUserCookie(userInfo, -1, "web"); return(Redirect("/")); } else { return(PartialView("用户创建失败")); } } }
/// <summary> /// 订阅宝SSO登录 /// </summary> public ActionResult DybLogin() { try { LogUtil.WriteLog($"开始登陆 url:{WebHelper.GetRawUrl()}"); if (WorkContext.Uid > 0) { WebHelper.DeleteCookie("bma"); Sessions.RemoverSession(WorkContext.Sid); OnlineUsers.DeleteOnlineUserBySid(WorkContext.Sid); } string username = WebHelper.GetQueryString("username"); string agentname = WebHelper.GetQueryString("agentname"); string redirectUrl = WebHelper.GetQueryString("redirect"); string ak = WebHelper.GetQueryString("ak"); if (string.IsNullOrEmpty(username)) { return(PromptView("该单点登录链接缺少用户信息")); } var storepid = 0; if (!string.IsNullOrEmpty(agentname)) { var agentUid = OAuths.GetUidByOpenIdAndServer(agentname, "dyb"); if (agentUid > 0) { var agentUser = Users.GetPartUserById(agentUid); storepid = agentUser.StoreId; } } var uid = OAuths.GetUidByOpenIdAndServer(username, "dyb"); if (uid < 1) { //创建用户 var userInfo = OAuths.CreateOAuthUser(DateTime.Now.ToString("yyMMddHHmmssms"), "dyb", username, "dyb", WorkContext.RegionId); uid = userInfo.Uid; } var partUserInfo = Users.GetPartUserById(uid); if (partUserInfo.StoreId == 0) { //创建店铺 var storeId = AdminStores.CreateStore(new StoreInfo { Announcement = "", Banner = "", CreateTime = DateTime.Now, DePoint = 10, Description = "", Honesties = 0, Logo = "", Mobile = "", Name = "微信小程序", Phone = "", QQ = "", RegionId = 0, SePoint = 10, ShPoint = 10, State = 0, StateEndTime = DateTime.Now.AddYears(1), StoreIid = 0, StoreRid = 1, Theme = "default", WW = "", StorePid = storepid }, new StoreKeeperInfo { Address = username, IdCard = username, Name = partUserInfo.UserName, Type = 0 }); AdminStores.CreateStoreShipTemplate(new StoreShipTemplateInfo { Free = 1, StoreId = storeId, Title = "全场包邮", Type = 0 }); //创建会员等级 AdminUserRanks.CreateUserRank(new UserRankInfo { Avatar = string.Empty, CreditsLower = 0, CreditsUpper = -1, LimitDays = 0, StoreId = storeId, System = 0, Title = "初级会员" }); partUserInfo.StoreId = storeId; partUserInfo.UserRid = UserRanks.GetLowestUserRank(storeId).UserRid; WorkContext.StoreId = storeId; Users.UpdatePartUser(partUserInfo); } //如果对应的店铺没有平台ID,则可以更改storepid,否则不行 if (storepid > 0) { var store = Stores.GetStoreById(partUserInfo.StoreId); if (store != null && store.StorePid == 0 && store.StoreId != storepid) { AdminStores.UpdateStorePId(store.StoreId, storepid); } } //将用户信息写入cookie中 MallUtils.SetUserCookie(partUserInfo, 365); StoreInfoManager.UpdateDistributor(partUserInfo.StoreId, ak.Equals("dis", StringComparison.InvariantCultureIgnoreCase)); LogUtil.WriteLog($"开始登陆 开始跳转"); if (string.IsNullOrEmpty(redirectUrl)) { return(RedirectToAction("index", "home", new RouteValueDictionary { { "area", "storeadmin" } })); } else { return(Redirect(redirectUrl)); } } catch (Exception e) { Trace.TraceError(e.Message); throw; } }
public Guid AccessToken() { return(OAuths.GetAccessToken(this)); }