网络组件增加身份令牌验证,令牌可由用户自己指定。模版使用了自定义的令牌

This commit is contained in:
hsl20
2017-03-05 18:32:55 +08:00
parent ea459616d7
commit 1e249baa1d
10 changed files with 43 additions and 53 deletions

View File

@@ -9,6 +9,10 @@ namespace CommonLibrary
{
#region
//======================================================================================
// 此处的所有的网络端口应该重新指定,防止其他人的项目连接到你的程序上
// 假设你们的多个项目服务器假设在一台电脑的情况,就绝对要替换下面的端口号
/// <summary>
/// 主网络端口,此处随机定义了一个数据
/// </summary>

View File

@@ -10,6 +10,12 @@ namespace CommonLibrary
/// </summary>
public class CommonHeadCode
{
/// <summary>
/// 用于同步和异步的网络的身份令牌,提升安全性
/// </summary>
public static Guid KeyToken { get; set; } = new Guid("1275BB9A-14B2-4A96-9673-B0AF0463D474");
/// <summary>
/// 同步通信的头说明以字母A开头
/// </summary>

View File

@@ -183,6 +183,11 @@
网络访问中的超时时间单位毫秒默认值2200
</summary>
</member>
<member name="P:IndustryEthernet.SocketBase.KeyToken">
<summary>
该网络通信验证类的身份令牌
</summary>
</member>
<member name="M:IndustryEthernet.SocketBase.ThreadPoolCheckConnect(System.Object)">
<summary>
检查超时的子线程
@@ -310,16 +315,6 @@
服务器客户端约定的暗号协议
</summary>
</member>
<member name="F:IndustryEthernet.Hu_Net_Protocol.HU_APP_ID">
<summary>
异步通信唯一识别的暗号代码为16个Byte字符组
</summary>
</member>
<member name="F:IndustryEthernet.Hu_Net_Protocol.HU_Simplify_Net">
<summary>
同步通信中用于识别的唯一的暗号代码
</summary>
</member>
<member name="P:IndustryEthernet.Hu_Net_Protocol.HU_Protocol_Check_Secends">
<summary>
用于心跳程序的暗号信息

Binary file not shown.

View File

@@ -1,2 +1,2 @@
# C-S架构的服务器客户端模版
该模版实现了基础的账户管理功能,版本控制,软件升级,公告管理,消息群发功能。
# C-S架构的服务器客户端模版
该模版由三部分的程序组成,一个服务端运行的程序,一个客户端运行的程序,还有一个公共的组件,实现了基础的账户管理功能,版本控制,软件升级,公告管理,消息群发功能。具体的操作方法见演示就行

View File

@@ -127,11 +127,7 @@ namespace 软件系统客户端模版
#region
/// <summary>
/// 用于访问服务器数据的网络对象类,必须修改这个端口参数,否则运行失败
/// </summary>
private Net_Simplify_Client net_simplify_client = new Net_Simplify_Client(
new System.Net.IPEndPoint(System.Net.IPAddress.Parse(UserClient.ServerIp), CommonLibrary.CommonLibrary.Port_Second_Net));
/// <summary>
/// 用于验证的后台线程
@@ -169,7 +165,7 @@ namespace 软件系统客户端模版
Thread.Sleep(200);
//请求指令头数据,该数据需要更具实际情况更改
OperateResultString result = net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.);
OperateResultString result = UserClient.Net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.);
if(result.IsSuccess)
{
//例如返回结果为1说明允许登录0则说明服务器处于维护中并将信息显示
@@ -195,7 +191,7 @@ namespace 软件系统客户端模版
//延时
Thread.Sleep(200);
result = net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.);
result = UserClient.Net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.);
if (result.IsSuccess)
{
//服务器应该返回服务器的版本号
@@ -234,7 +230,7 @@ namespace 软件系统客户端模版
json.Add(BasicFramework.UserAccount.UserNameText, new Newtonsoft.Json.Linq.JValue(textBox_userName.Text));
json.Add(BasicFramework.UserAccount.PasswordText, new Newtonsoft.Json.Linq.JValue(textBox_password.Text));
result = net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.+json.ToString());
result = UserClient.Net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.+json.ToString());
if (result.IsSuccess)
{
//服务器应该返回账户的信息
@@ -273,7 +269,7 @@ namespace 软件系统客户端模版
Thread.Sleep(200);
result = net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.);
result = UserClient.Net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.);
if(result.IsSuccess)
{
//服务器返回初始化的数据,此处进行数据的提取,有可能包含了多个数据

View File

@@ -119,7 +119,7 @@ namespace 软件系统客户端模版
p => {
JObject json = new JObject();json.Add(UserAccount.UserNameText, UserClient.UserAccount.UserName);
json.Add(UserAccount.PasswordText, p);
return net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode. + json.ToString()).IsSuccess;
return UserClient.Net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode. + json.ToString()).IsSuccess;
}, 6, 8);
fpm.ShowDialog();
fpm.Dispose();
@@ -155,7 +155,7 @@ namespace 软件系统客户端模版
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormInputAndAction fiaa = new FormInputAndAction(str => net_simplify_client.ReadFromServer(
FormInputAndAction fiaa = new FormInputAndAction(str => UserClient.Net_simplify_client.ReadFromServer(
CommonHeadCode.SimplifyHeadCode. + str).IsSuccess,UserClient.Announcement);
fiaa.ShowDialog();
fiaa.Dispose();
@@ -163,14 +163,14 @@ namespace 软件系统客户端模版
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormLog flg = new FormLog(net_simplify_client);
FormLog flg = new FormLog(UserClient.Net_simplify_client);
flg.ShowDialog();
flg.Dispose();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
using (FormRegisterAccount fra = new FormRegisterAccount(net_simplify_client))
using (FormRegisterAccount fra = new FormRegisterAccount(UserClient.Net_simplify_client))
{
fra.ShowDialog();
}
@@ -180,10 +180,10 @@ namespace 软件系统客户端模版
{
FormAccountManage fam = new FormAccountManage(() =>
{
OperateResultString result = net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode.);
OperateResultString result = UserClient.Net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode.);
if (result.IsSuccess) return result.Content;
else return result.ToMessageShowString();
},m => net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode. + m).IsSuccess);
},m => UserClient.Net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode. + m).IsSuccess);
fam.ShowDialog();
fam.Dispose();
}
@@ -215,6 +215,7 @@ namespace 软件系统客户端模版
{
try
{
net_socket_client.KeyToken = CommonHeadCode.KeyToken;//新增的身份令牌
net_socket_client.EndPointServer = new System.Net.IPEndPoint(
System.Net.IPAddress.Parse(UserClient.ServerIp),
CommonLibrary.CommonLibrary.Port_Main_Net);
@@ -308,23 +309,5 @@ namespace 软件系统客户端模版
#endregion
#region
//=========================================================================================
//
// 在本界面任意地方调用net_simplify_client.ReadFromServer("[指令头]")即可获取服务器数据
// 处理结果之前先进行判定,具体参照示例
//
//=========================================================================================
/// <summary>
/// 用于界面请求访问服务器数据所用
/// </summary>
private Net_Simplify_Client net_simplify_client = new Net_Simplify_Client(
new System.Net.IPEndPoint(System.Net.IPAddress.Parse(UserClient.ServerIp),
CommonLibrary.CommonLibrary.Port_Second_Net));
#endregion
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IndustryEthernet;
namespace
@@ -55,5 +56,16 @@ namespace 软件系统客户端模版
/// 当前系统的登录账户
/// </summary>
public static BasicFramework.UserAccount UserAccount { get; set; } = new BasicFramework.UserAccount();
/// <summary>
/// 用于访问服务器数据的网络对象类,必须修改这个端口参数,否则运行失败
/// </summary>
public static Net_Simplify_Client Net_simplify_client = new Net_Simplify_Client(
new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ServerIp),
CommonLibrary.CommonLibrary.Port_Second_Net))
{
KeyToken = CommonLibrary.CommonHeadCode.KeyToken,
};
}
}

View File

@@ -205,7 +205,6 @@ namespace 软件系统服务端模版
//在服务器的这个路径下放置客户端运行的所有文件不要包含settings文件不要从此处运行
//只放置exe和dll组件必须放置软件自动更新.exe
net_soft_update_Server.FileUpdatePath = @"C:\ClientFiles";
//根据指定端口启动服务,需要自己指定,本处为示例
net_soft_update_Server.AutoUpdateEngineStart(CommonLibrary.CommonLibrary.Port_Update_Net);
}
catch (Exception ex)
@@ -230,7 +229,6 @@ namespace 软件系统服务端模版
{
net_file_update.FilesPath = @"C:\ClientFiles";//服务器客户端需要更新的路径,与上述一致
net_file_update.log_record.log_save_path = Application.StartupPath + @"\update_file_log.txt";
//需要指定启动端口,本次为示例
net_file_update.Server_Start(CommonLibrary.CommonLibrary.Port_Update_Remote);
}
catch (Exception ex)
@@ -252,10 +250,10 @@ namespace 软件系统服务端模版
{
try
{
net_simplify_server.KeyToken = CommonHeadCode.KeyToken;//设置身份令牌
net_simplify_server.log_record.log_save_path = Application.StartupPath + @"\simplify_log.txt";
net_simplify_server.ReceiveStringEvent += Net_simplify_server_ReceiveStringEvent;
net_simplify_server.ReceivedBytesEvent += Net_simplify_server_ReceivedBytesEvent;
//应该指定特殊的端口,此处为示例
net_simplify_server.Server_Start(CommonLibrary.CommonLibrary.Port_Second_Net);
}
catch (Exception ex)
@@ -385,6 +383,7 @@ namespace 软件系统服务端模版
{
try
{
net_socket_server.KeyToken = CommonHeadCode.KeyToken;//设置身份令牌
net_socket_server.LogReacord.log_save_path = Application.StartupPath + @"\net_log.txt";
net_socket_server.IsSaveLogClientLineChange = true;
net_socket_server.ClientOnline += Net_socket_server_ClientOnline;
@@ -392,7 +391,6 @@ namespace 软件系统服务端模版
net_socket_server.MessageAlerts += Net_socket_server_MessageAlerts;
net_socket_server.AcceptByte += Net_socket_server_AcceptByte;
net_socket_server.AcceptString += Net_socket_server_AcceptString;
//测试端口,应根据实际使用时指定
net_socket_server.SocketStart(CommonLibrary.CommonLibrary.Port_Main_Net);
}
catch (Exception ex)

View File

@@ -52,10 +52,6 @@ namespace 软件系统服务端模版
/// </summary>
public class UserAccountEx : UserAccount
{
/// <summary>
/// 账户的设备管理类别
/// </summary>
public string Group { get; set; } = "";
/// <summary>
/// 用户的年龄
/// </summary>