public string Login(string username, string userpwd)
        {
            //1.校验
            dynamic user = Dbop.Get(username)?.ToDynamic();

            if (user == null)
            {
                return("Error: User not exist.Please <a href='javascript:history.go(-1)'>go backward</a>.");
            }


            if (user.userPwd != EncryptHelper.SHA256(userpwd))
            {
                return("Error: Wrong password.Please <a href='javascript:history.go(-1)'>go backward</a>.");
            }

            //2.给token
            string output = $"{username}|{DateTime.Now.ToString("yyyy-MM-dd")}";
            string token  = EncryptHelper.AES_Encrypt(output);

            return(string.Format(@"
<html>
<head><script>
document.cookie='NSPTK={0}; path=/;';
document.write('Redirecting...');
window.location.href='main.html';
</script>
</head>
</html>
            ", token));
        }
Beispiel #2
0
        public string Login(string username, string userpwd, string ip)
        {
            //1.校验
            dynamic user = Dbop.Get(username)?.ToDynamic();

            if (user == null)
            {
                return("错误: 用户不存在。请点击<a href='javascript:history.go(-1)'>此处</a>返回。");
            }


            if (user.userPwd != EncryptHelper.SHA256(userpwd))
            {
                return("错误: 密码不正确。请点击<a href='javascript:history.go(-1)'>此处</a>返回。");
            }
            ServerContext.logger.Info($"用户{username}登录成功,IP:{ip}");
            //2.给token
            string output = $"{username}|{DateTime.Now.ToString("yyyy-MM-dd")}|{user["role"].Value}";
            string token  = EncryptHelper.AES_Encrypt(output);

            return(string.Format(@"
<html>
<head><script>
document.cookie='NSPTK={0}; path=/;';
document.cookie='ROLE={1}; path=/;';
document.cookie='UNAME={2}; path=/;';
document.write('Redirecting...');
window.location.href='main.html';
</script>
</head>
</html>
            ", token, user["role"].Value, username));
        }
Beispiel #3
0
        private void Mail(string title, Func <string, string> replace, string receiver = null)
        {
            var value = sysDB.Get(SystemKey.EMAIL.ToString());

            if (!string.IsNullOrEmpty(value))
            {
                var mail = JsonConvert.DeserializeObject <MailAccount>(value);
                if (mail.enable)
                {
                    EmailHelper emailHelper = new EmailHelper(mail.smtpServer, mail.smtpPort, mail.userName, mail.password);
                    string      content     = replace(mail.content);
                    if (!string.IsNullOrEmpty(receiver))
                    {
                        emailHelper.SendEmail(receiver, title, content, taskManager.logger);
                    }
                    else
                    {
                        var users = userDB.Select(0, 10).Select(p => JsonConvert.DeserializeObject <User>(p));
                        foreach (var u in users)
                        {
                            if (u.receiveEmail && !string.IsNullOrEmpty(u.email))
                            {
                                emailHelper.SendEmail(u.email, title, content, taskManager.logger);
                            }
                        }
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// 通过token获取clientid
        /// 返回0说明失败,返回-1说明用户被禁
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        private async Task <int> GetClientIdFromNextTokenBytes(TcpClient client)
        {
            NetworkStream nstream           = client.GetStream();
            int           clientIdFromToken = 0;
            //1.1 获取token长度
            int tokenLengthLength = 2;

            byte[] tokenLengthBytes = new byte[tokenLengthLength];
            int    resultByte01     = await nstream.ReadAsyncEx(tokenLengthBytes);

            Server.Logger.Debug("tokenLengthBytes received.");
            if (resultByte01 < 1)
            {
                CloseClient(client);
                return(0);
            }

            //1.2 获取token
            int tokenLength = StringUtil.DoubleBytesToInt(tokenLengthBytes);

            byte[] tokenBytes   = new byte[tokenLength];
            int    resultByte02 = await nstream.ReadAsyncEx(tokenBytes);

            Server.Logger.Debug("tokenBytes received.");
            if (resultByte02 < 1)
            {
                CloseClient(client);
                return(0);
            }

            string token = tokenBytes.ToASCIIString();

            if (token != Global.NO_TOKEN_STRING)
            {
                var tokenClaims = StringUtil.ConvertStringToTokenClaims(token);
                var userJson    = DbOp.Get(tokenClaims.UserKey);
                if (userJson == null)
                {
                    Server.Logger.Debug("token验证失败");
                }
                else
                {
                    var userId = userJson.ToObject <User>().userId;
                    if (ServerContext.ServerConfig.BoundConfig.UsersBanlist.Contains(userId))
                    {
                        Server.Logger.Debug("用户被禁用");
                        return(-1);
                    }
                    else
                    {
                        clientIdFromToken = int.Parse(userId);
                    }
                }
            }

            return(clientIdFromToken);
        }
Beispiel #5
0
        public string GetEmailAccount()
        {
            var data = SysDB.Get(SystemKey.EMAIL.ToString());

            if (data == null)
            {
                return(MailAccount.Default().ToJsonString());
            }
            return(data);
        }