protected void Application_Start(object sender, EventArgs e) { log4net.Config.XmlConfigurator.Configure(); AppServerDataInitializer.Init(); SessionCenter.Start(); //SessionCenter.EmailTiming(); }
static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); AppServerDataInitializer.Init(); var uri = new Uri(System.Configuration.ConfigurationManager.AppSettings["Server"]); using (var host = new NancyHost(uri)) { host.Start(); SessionCenter.Start(); //SessionCenter.EmailTiming(); -- 邮件定时发送 Console.WriteLine("Your application is running on " + uri); Console.WriteLine("Press any [Enter] to close the host."); Console.ReadLine(); SessionCenter.Stop(); } }
public AuthModule() : base("Auth") { Get["/GetLog/{date}"] = _ => { try { int d = _.date; var response = new Response(); response.Headers.Add("Content-Disposition", "attachment; filename=log.txt"); response.ContentType = "application/octet-stream"; string dir = Path.Combine(AppPath.LogFolder, d.ToString()); if (Directory.Exists(dir)) { string[] files = Directory.GetFiles(dir, "*.txt"); if (files.Length > 0) { byte[] buffer = File.ReadAllBytes(files[0]); response.Contents = stream => { using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write(buffer); } }; return(response); } else { return(null); } } else { return(null); } } catch { return(null); } }; Post["/Login"] = _ => { dynamic data = FetchFormData(); string loginname = data.LoginName; string pwd = data.Pwd; string msg = string.Empty; bool state = authobj.CheckUserExist(loginname); if (!state) { return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 0, Msg = "你所登录的账号不存在!" })); } UserSessionEntity ue = authobj.TryLogin(loginname, pwd, out msg); if (ue != null) { string sessionid = SessionCenter.AddSessionIdentity(ue); if (!string.IsNullOrEmpty(sessionid)) { ue.SessionID = sessionid; return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 1, Msg = "登录成功", Value = ue })); } else { return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 0, Msg = "创建登录会话失败,请稍后重试!" })); } } else { return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 0, Msg = msg })); } }; Post["/LoginX"] = _ => { dynamic data = FetchFormData(); string thirdid = data.ThirdID; //Request.Form.ThirdID; string username = data.ThirdUserName; //Request.Form.ThirdUserName; int type = data.ThirdType; //Request.Form.ThirdType; string picurl = data.Url; UserSessionEntity ue = authobj.TryLoginX(username, thirdid, type, picurl); if (ue != null) { string sessionid = SessionCenter.AddSessionIdentity(ue); if (!string.IsNullOrEmpty(sessionid)) { ue.SessionID = sessionid; return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 1, Msg = "登录成功", Value = ue })); } else { return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 0, Msg = "创建登录会话失败,请稍后重试!" })); } } else { return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 0, Msg = "登录失败" })); } }; Post["/Register"] = _ => { try { dynamic data = FetchFormData(); string phone = data.Phone; string code = data.VCode; string pwd = data.Pwd; string msg = string.Empty; string nickName = string.Empty; int userid = authobj.RegesiterNewUser(phone, pwd, code, out msg, out nickName); if (userid > 0) { UserSessionEntity ue = new UserSessionEntity(); ue.UserID = userid; ue.ZoneID = 0; ue.PicturePath = AppUtil.UserDefaultHeader + AppUtil.ConvertJpg; string sessionid = SessionCenter.AddSessionIdentity(ue); if (!string.IsNullOrEmpty(sessionid)) { ue.SessionID = sessionid; ue.NickName = nickName; return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 1, Msg = "注册成功", Value = ue })); } else { return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 0, Msg = "创建登录会话失败,请稍后重试!" })); } } else { return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 0, Msg = msg })); } } catch (Exception ex) { LogUtil.WriteLog(ex); return(JsonObj <JsonMessageBase <UserSessionEntity> > .ToJson(new JsonMessageBase <UserSessionEntity>() { Status = 0, Msg = "注册失败" })); } }; Post["/CheckUserExist"] = _ => { try { dynamic data = FetchFormData(); string loginname = data.LoginName; bool state = authobj.CheckUserExist(loginname); return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 1, Msg = "检测成功", Tag = state.ToString() })); } catch (Exception ex) { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = ex.Message })); } }; Post["/SendVCode"] = _ => { try { dynamic data = FetchFormData(); string phone = data.Phone; string msg = string.Empty; if (SmsMananger.SendCode(phone, out msg)) { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 1, Msg = msg })); } else { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = msg })); } } catch (Exception ex) { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = ex.Message })); } }; Post["/ResetMyPassword"] = _ => { try { dynamic data = FetchFormData(); string phone = data.Phone; string vcode = data.VCode; string pwd = data.Pwd; string msg = string.Empty; if (authobj.ResetPassword(phone, vcode, pwd, out msg)) { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 1, Msg = "重置密码成功" })); } else { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = string.IsNullOrEmpty(msg) ? "重置失败" : msg })); } } catch (Exception ex) { LogUtil.WriteLog(ex); return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = ex.Message })); } }; Get["/CheckAppUpdateState"] = _ => { try { var data = FecthQueryData(); int appid = data.AppType; string ver = data.Version; int versionType = 0; try { versionType = data.VersionType; } catch { versionType = 0; } AppUpdateEntity info = new AppUpdateEntity(); bool state = false; if (versionType == 0) { state = commonobj.CheckAppUpdateState(appid, ver, out info); } else { state = commonobj.CheckAppUpdateState(appid, ver, versionType, out info); } if (state) { info.NeedUpdate = true; return(JsonObj <JsonMessageBase <AppUpdateEntity> > .ToJson(new JsonMessageBase <AppUpdateEntity>() { Status = 1, Msg = "有新版本", Tag = "1", Value = info })); } else { return(JsonObj <JsonMessageBase <AppUpdateEntity> > .ToJson(new JsonMessageBase <AppUpdateEntity>() { Status = 1, Msg = "程序为最新版不需要更新", Tag = "0", Value = new AppUpdateEntity() })); } } catch (Exception ex) { return(JsonObj <JsonMessageBase <AppUpdateEntity> > .ToJson(new JsonMessageBase <AppUpdateEntity>() { Status = 0, Msg = ex.Message, Value = new AppUpdateEntity() })); } }; #region 改变客户签到状态 2015/11/12 Get["UpdateSignState"] = _ => { dynamic data = FecthQueryData(); string customerKey = data.CustomerKey; string stateStr = data.State; int state = 0; int.TryParse(stateStr, out state); IList <SignBookEntity> entity = BCtrl_SignBook.Instance.SignBook_Get(customerKey); if (entity == null) { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = "数据查询异常,无法连接服务器" })); } else if (entity.Count == 0) { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = "不是有效报名用户,不能签到!" })); } else if (entity[0].IsSign == 1) { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = " 重复签到 " + entity[0].Company + " " + entity[0].Customer })); } bool flag = BCtrl_SignBook.Instance.SignBook_Update(customerKey, state); if (!flag) { return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = "数据查询异常,无法连接服务器" })); } return(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 1, Msg = entity[0].Company + " " + entity[0].Customer + " 签到成功!" })); }; #endregion }
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines) { string gzipflag = System.Configuration.ConfigurationManager.AppSettings["EnableGzip"] ?? "1"; if (gzipflag == "1") { AddGZip(pipelines); } pipelines.BeforeRequest += ctx => { bool valid = false; string msg = string.Empty; try { //LogUtil.WriteLog(ctx.Request.Url.ToString()); //放过登录入口 string url = ctx.Request.Path; IList <string> freePass = new List <string>();//免除通行证 freePass.Add("/Auth/"); freePass.Add("/Exhibition/"); freePass.Add("/SignBook/"); freePass.Add("/DialogMessages/"); freePass.Add("/Article/ArticleCommon"); for (int i = 0; i < freePass.Count; i++) { if (url.Contains(freePass[i])) { return(null); } } string appkey = ctx.Request.Headers["mars_appkey"].FirstOrDefault(); //LogUtil.WriteLog(appkey==null ? "NULL" : appkey); //LogUtil.WriteLog(AppServerDataInitializer.AppClients.Count > 0 ? AppServerDataInitializer.AppClients[appkey].AppKey : ""); if (appkey != null && AppServerDataInitializer.AppClients.ContainsKey(appkey)) { string token = ctx.Request.Headers["mars_token"].FirstOrDefault(); string sessionid = ctx.Request.Headers["mars_sid"].FirstOrDefault(); string tick = ctx.Request.Headers["mars_tick"].FirstOrDefault(); string version = ctx.Request.Headers["mars_version"].FirstOrDefault(); string method = ctx.Request.Method.ToLower(); if (!string.IsNullOrEmpty(token)) { SessionIdentity si = SessionCenter.GetIdentity(sessionid); if (si != null) { string data = method == "get" ? ctx.Request.Query.data : ctx.Request.Form.data; string token1 = GetSign(data, tick, appkey, sessionid, version, AppServerDataInitializer.AppClients[appkey].AppSecrect); StringBuilder sblog = new StringBuilder(); sblog.AppendFormat(",Path:{0}", ctx.Request.Url.Path); sblog.AppendFormat(",Method:{0}", method); sblog.AppendFormat(",Data:", data); sblog.AppendFormat(",Tick:{0}", tick); sblog.AppendFormat(",AppKey:{0}", appkey); sblog.AppendFormat(",SessionID:{0}", sessionid); sblog.AppendFormat(",Version:{0}", version); sblog.AppendFormat(",AppSecrect:{0}", AppServerDataInitializer.AppClients[appkey].AppSecrect); sblog.AppendFormat(",Token:{0}", token1); sblog.AppendFormat(",TokenFromClient:{0}", token); LogUtil.WriteLog(sblog.ToString()); if (token == token1) { si.Version = version; si.AppKey = appkey; MarsUserIdentity identity = new MarsUserIdentity(); identity.SessionID = si.SessionID; ctx.CurrentUser = identity; valid = true; } else { msg = "请求密钥错误!"; } } else { msg = "非法会话ID,请退出系统重新登录"; } } else { msg = "缺少会话密钥"; } } else { msg = "非法AppKey"; } } catch (Exception ex) { msg = ex.Message; LogUtil.WriteLog(ex); } if (!valid) { var res = new Response(); res.ContentType = "application/json; charset=utf-8"; res.Contents = s => { byte[] bs = Encoding.UTF8.GetBytes(JsonObj <JsonMessageBase> .ToJson(new JsonMessageBase() { Status = 0, Msg = msg })); s.Write(bs, 0, bs.Length); }; return(res); } return(null); }; base.ApplicationStartup(container, pipelines); }
protected void Application_End(object sender, EventArgs e) { SessionCenter.Stop(); }