/// <summary> /// url 已经登录的用户 如果状态变更为不可用状态即跳转到指定页面 /// </summary> /// <param name="userInfo">用户信息</param> /// <returns></returns> public static SSOData LoginRoute(SSOUser userInfo) { try { var list = CacheHelper.Item_Get<List<Filters>>(Stands.FILTERS); if (list != null && list.Any(x => x.FlagValue == userInfo.FlagValue)) { var redirect = list.First(x => x.FlagValue == userInfo.FlagValue); return new SSOData { IsLogin = true, IsRedirect = true, Url = redirect.Url, User = userInfo }; } return new SSOData { User = userInfo, IsLogin = true }; } catch (Exception ex) { return new SSOData { ErrorCode = "error", ErrorMessage = ex.Message, Url = "/" }; } }
/// <summary> /// url 权限路由 /// </summary> /// <param name="callBack"></param> /// <param name="userInfo">用户信息</param> /// <returns></returns> public static AuthMessage RouteUrl(string callBack, SSOUser userInfo) { return RouteUrl(callBack, userInfo, null); }
public static void SaveLoginInfo(SSOUser userInfo, string token) { //token = token.ToUpper(); //主站存Cookie Tools.SetCookie(Stands.UID, userInfo.Uid); Tools.SetCookie(Stands.TOKEN, token); Tools.SetCookie(Stands.CURRENT_PROJECT_CODE_KEY, userInfo.ProjectCode); //Redis 存储 try { var key = userInfo.ProjectCode + "_" + token; _log.Info(String.Format(" SET {0} {1} RouteUtils>SaveLoginInfo", key, userInfo)); CacheHelper.Item_Set(key, userInfo); CacheHelper.SortedSet_SetExpire(key, DateTime.Now.AddDays(1)); //one day } catch (Exception ex) { _log.Error(ex.Message); throw new Exception("缓存服务器启动异常:" + ex.Message); } /*当前版本的redis 不支持expire key field 接下来可能会更新*/ //CacheHelper.Hash_Set(userInfo.ProjectCode, token, userInfo); //CacheHelper.Hash_SetExpire(userInfo.ProjectCode, DateTime.Now.AddDays(7)); }
/// <summary> /// url 权限路由 /// </summary> /// <param name="callBack"></param> /// <param name="_biz">数据请求对象</param> /// <param name="_client">url编辑对象</param> /// <param name="userInfo">用户信息</param> /// <param name="dic">其他信息</param> /// <returns></returns> public static AuthMessage RouteUrl(string callBack, CJClient _client, SSOUser userInfo, CJDictionary dic) { try { userInfo.Password = null; dic = dic ?? new CJDictionary(); //尝试从cookie中拿openid var openId = Tools.GetCookie(Stands.OpenIdCookie); //生成主站令牌 ps:如果有openid 使用openid作为令牌,如果没有openid 使用id var token = string.IsNullOrEmpty(openId) ? Guid.NewGuid().ToString().ToUpper() : openId; dic[CJClient.TOKEN] = token; var list = CacheHelper.Item_Get<List<Filters>>(Stands.FILTERS); //存储用户信息 SaveLoginInfo(userInfo, token); try { string configMsg; if (list != null && list.Any(x => x.FlagValue == userInfo.FlagValue)) { var redirect = list.First(x => x.FlagValue == userInfo.FlagValue); callBack = redirect.Url; configMsg = redirect.Message; } else { callBack = _client.BuildReturnUrl(callBack, dic); configMsg = "登录成功!"; } return new AuthMessage { Message = configMsg, Url = callBack, Status = userInfo.FlagValue }; } catch (Exception ex) { _log.Error(ex.Message); throw new Exception("请检查xml文件:" + ex.Message); } } catch (Exception ex) { _log.Error(ex.Message); return new AuthMessage { IsError = true, Message = ex.Message }; } }
/// <summary> /// url 权限路由 /// </summary> /// <param name="callBack"></param> /// <param name="userInfo">用户信息</param> /// <param name="dic">其他信息 该信息会通过Url返回到分站的callback</param> /// <returns></returns> public static AuthMessage RouteUrl(string callBack, SSOUser userInfo, CJDictionary dic) { CJClient _client = new CJClient(); return RouteUrl(callBack, _client, userInfo, dic); }
// // GET: /SSOAuthorize/MyCallBack/ public override void CallBacking(string url, SSOUser user) { Session["User"] = user; base.CallBacking(url, user); }
public ActionResult UserInfoCallback(string code, string state) { try { if (string.IsNullOrEmpty(code)) { return Content("您拒绝了授权!"); } if (state == null || state != "cj_jason_sso") { return Content("验证失败!请从正规途径进入!"); } OAuthAccessTokenResult result = null; //通过,用code换取access_token try { result = OAuthApi.GetAccessToken(_appId, _secret, code); Tools.SetCookie(Stands.OpenIdCookie, result.openid, DateTime.Now.AddYears(1)); Tools.SetCookie(Stands.AccessToken, result.access_token, DateTime.Now.AddYears(1)); } catch (Exception ex) { _log.Error(ex); return Content("服务器繁忙"); } if (result.errcode != ReturnCode.请求成功) { _log.Error(result.errmsg); return Content("错误:" + result.errmsg); } try { //callback&projectcode ////因为第一步选择的是OAuthScope.snsapi_userinfo,这里可以进一步获取用户详细信息 OAuthUserInfo userInfo = OAuthApi.GetUserInfo(result.access_token, result.openid); SSOUser info = new SSOUser { Avatar = userInfo.headimgurl }; var projectCode = Tools.GetCookie(Stands.CURRENT_PROJECT_CODE_KEY); var key = projectCode + "_" + userInfo.openid.ToUpper(); var model = CacheHelper.Item_Get<SSOUser>(key); // Tools.Log(String.Format(" GET {0} {1} AuthorizeController>UserInfoCallback", key, model)); if (model == null) { var dic = new CJDictionary { { CJClient.PROJECT_CODE, projectCode }, { CJClient.AUTH_TYPE, (int)AuthType.Weixin }, { CJClient.AVATAR, info.Avatar }, { CJClient.OPEN_ID,userInfo.openid}}; //把参数存到session return Redirect(_client.BuildUrl(Stands.AUTH_HOST + loginPage, Stands.SIGN_SECRET, dic)); } var callBack = Tools.GetCookie(projectCode + "_CallBack"); var authMessage = RouteUtils.RouteUrl(callBack, _client, model, null); return Redirect(authMessage.Url); } catch (ErrorJsonResultException ex) { _log.Error(ex); return Content(ex.Message); } } catch (Exception ex) { _log.Error(ex); return Content("服务器繁忙。。。"); } }
/// <summary> /// 回调函数中执行的用户自定义方法 /// </summary> /// <param name="url"></param> /// <param name="user"></param> public virtual void CallBacking(string url, SSOUser user) { }