public async Task <string> CheckToken(LyciumToken token)
 {
     if (IsLegal)
     {
         var localToken = _tokenService.GetToken(Uid, Gid);
         if (localToken == null)
         {
             return(null);
         }
         if (token.Content == localToken.Content)
         {
             return(JsonResult(localToken));
         }
         else
         {
             return(null);
         }
     }
     else
     {
         HttpContext.Response.StatusCode = 401;
         LyciumConfiguration.ReturnMessage(HttpContext, 4016, "主机校验不合法!");
         return(null);
     }
 }
        public async Task <string> LoginToken()
        {
            if (IsLegal)
            {
                bool needAdd = false;
                //已经存在token
                var token = _tokenService.GetToken(Uid, Gid);
                if (token == null)
                {
                    needAdd   = true;
                    token     = new LyciumToken();
                    token.Uid = Uid;
                    token.Gid = Gid;
                }

                //重置 Token 内容
                TokenOperator.Flush(token);
                if (needAdd)
                {
                    //如果数据库没有则新增
                    if (_tokenService.AddToken(token))
                    {
                        //如果新增成功则发送通知
                        await _tokenNotify.NotifyTokensAdd(Gid, token);

                        return(JsonResult(token));
                    }
                    else
                    {
                        HttpContext.Response.StatusCode = 500;
                        LyciumConfiguration.ReturnMessage(HttpContext, 4017, "无法创建服务端Token,请联系管理员!");
                    }
                }
                else
                {
                    //如果数据库有则更新
                    if (_tokenService.ModifyToken(token))
                    {
                        //如果修改成功则发送通知
                        await _tokenNotify.NotifyTokensModify(Gid, token);

                        return(JsonResult(token));
                    }
                    else
                    {
                        HttpContext.Response.StatusCode = 401;
                        LyciumConfiguration.ReturnMessage(HttpContext, 4018, "无法更新服务端Token,请联系管理员!");
                    }
                }
            }
            else
            {
                HttpContext.Response.StatusCode = 401;
                LyciumConfiguration.ReturnMessage(HttpContext, 4016, "主机校验不合法!");
            }

            return(null);
        }
 public async Task <string> GetToken()
 {
     if (IsLegal)
     {
         return(JsonResult(_tokenService.GetToken(Uid, Gid)));
     }
     else
     {
         HttpContext.Response.StatusCode = 401;
         LyciumConfiguration.ReturnMessage(HttpContext, 4016, "主机校验不合法!");
         return(null);
     }
 }
        public async Task <string> RefreshToken(LyciumToken token)
        {
            if (IsLegal)
            {
                var localToken = _tokenService.GetToken(Uid, Gid);
                if (localToken == null)
                {
                    return(null);
                }
                if (token.Content == localToken.Content)
                {
                    if (TokenOperator.CanFlush(token))
                    {
                        TokenOperator.Flush(token);
                        if (_tokenService.ModifyToken(token))
                        {
                            await _tokenNotify.NotifyTokensModify(Gid, token);

                            return(JsonResult(token));
                        }
                        else
                        {
                            //二次重试
                            if (_tokenService.ModifyToken(token))
                            {
                                await _tokenNotify.NotifyTokensModify(Gid, token);

                                return(JsonResult(token));
                            }
                            else
                            {
                                HttpContext.Response.StatusCode = 500;
                                LyciumConfiguration.ReturnMessage(HttpContext, 4211, "服务端Token更新失败!");
                                return(null);
                            }
                        }
                    }
                }
                return(null);
            }
            else
            {
                HttpContext.Response.StatusCode = 401;
                LyciumConfiguration.ReturnMessage(HttpContext, 4016, "主机校验不合法!");
                return(null);
            }
        }