void Login() { string token, name; long appID, timestamp; if (!Form.TryGet("token", out token) || !Form.TryGet("account", out name) || !Form.TryGet("app_id", out appID) || !Form.TryGet("timestamp", out timestamp)) { WrapResult(ResultCode.InvalidParam, "invalid param!"); return; } if (string.IsNullOrEmpty(name)) { WrapResult(ResultCode.InvalidParam, "账号不能为空!"); return; } if (string.IsNullOrEmpty(token)) { WrapResult(ResultCode.InvalidParam, "Token 不能为空!"); return; } var time = DateTimeExtension.ConvertFromTimestamp(timestamp); if (Math.Abs((DateTime.Now - time).TotalSeconds) > Interval.TotalSeconds) { WrapResult(ResultCode.InvalidParam, "Token 已过期!"); return; } var app = RowAdapter.LoadFirstOrDefault <App>(p => p.ID == appID && p.Deleted == false); if (app == null) { WrapResult(ResultCode.InvalidParam, "指定应用未找到!"); return; } if (!app.AccessRestriction.Security(Request.RemoteEndPoint.Address)) { WrapResult(ResultCode.InvalidParam, "限制访问!"); return; } var account = RowAdapter.LoadFirstOrDefault <Account>(p => p.Name == name); if (account == null) { WrapResult(ResultCode.InvalidParam, "指定帐号不存在!"); return; } if (!account.Available) { WrapResult(ResultCode.InvalidAction, "该帐号不可用!"); return; } account.CheckErrorReset(); if (account.TodayErrorTimes >= MaxErrorTimes) { WrapResult(ResultCode.InvalidAction, "您的账号已被限制登录!"); return; } var credentials = new Framework.Security.ServerCredentials(account.Password); var authorization = new Framework.Security.Authorization(name, timestamp, token); if (!credentials.Authenticate(authorization)) { account.TodayErrorTimes++; account.TotalErrorTimes++; account.Save(); WrapResult(ResultCode.InvalidAction, "无效的Token!"); return; } if (string.IsNullOrEmpty(account.Realname)) { WrapResult((byte)ResultError.Incompletion, "帐号信息不完善,请先完善帐号信息!"); return; } var auth = RowAdapter.LoadFirstOrDefault <Authorization>(p => p.OwnerID == account.ID && p.AppID == app.ID); if (auth == null) { auth = RowAdapter.Create <Authorization>(); auth.OwnerID = account.ID; auth.AppID = appID; } else { auth.Reset(); } if (account.TodayErrorTimes > 0) { account.ResetError(); account.Save(); } auth.Save(); var json = new JsonObject(); json["auth_token"] = auth.Token; json["expires_in"] = (auth.DeathLine - DateTime.Now).TotalSeconds; WrapResult(ResultCode.OK, json); }
void Token() { var appID = Form.Find <long>("app_id"); var name = Form.Find("account"); var timestamp = Form.Find <long>("timestamp"); var token = Form.Find("token"); if (string.IsNullOrEmpty(name)) { WrapResult(false, "账号不能为空!"); return; } if (string.IsNullOrEmpty(token)) { WrapResult(false, "Token 不能为空!"); return; } var time = DateTimeExtension.ConvertFromTimestamp(timestamp); if (Math.Abs((DateTime.Now - time).TotalSeconds) > Interval.TotalSeconds) { WrapResult(false, "Token 已过期!"); return; } var app = RowAdapter.LoadFirstOrDefault <App>(p => p.ID == appID && p.Deleted == false); if (app == null) { WrapResult(false, "not found app!"); return; } if (!app.AccessRestriction.Security(Request.RemoteEndPoint.Address)) { WrapResult(ResultCode.InvalidParam, "限制访问!"); return; } var account = RowAdapter.LoadFirstOrDefault <Account>(p => p.Name == name); if (account == null) { WrapResult(false, "指定帐号不存在!"); return; } if (!account.Available) { WrapResult(false, "该帐号不可用!"); return; } account.CheckErrorReset(); if (account.TodayErrorTimes >= MaxErrorTimes) { WrapResult(false, "您的账号已被限制登录!"); return; } var credentials = new Framework.Security.ServerCredentials(account.Password); var authorization = new Framework.Security.Authorization(name, timestamp, token); if (!credentials.Authenticate(authorization)) { account.TodayErrorTimes++; account.TotalErrorTimes++; account.Save(); WrapResult(false, "无效的Token!"); return; } if (account.TodayErrorTimes > 0) { account.ResetError(); account.Save(); } var access = RowAdapter.LoadFirstOrDefault <Access>(p => p.AppID == appID && p.OwnerID == account.ID); if (access == null) { access = RowAdapter.Create <Access>(); access.OwnerID = account.ID; access.AppID = appID; } access.Reset(); access.Save(); var json = new JsonObject(); json["access_token"] = access.AccessToken; json["expires_in"] = (access.DeathLine - DateTime.Now).TotalSeconds; WrapResult(true, json); }