LoginForm - Check IP Address Input.
This commit is contained in:
parent
728f65cd90
commit
74855b6467
|
@ -20,7 +20,7 @@ namespace AIParkingApplication
|
||||||
private EngineApiController engineApiController;
|
private EngineApiController engineApiController;
|
||||||
private string doorAccessControlDeviceIP;
|
private string doorAccessControlDeviceIP;
|
||||||
|
|
||||||
public AIParkingApplicationForm(ApiController apiController, Config configOnWeb)
|
public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
this.apiController = apiController;
|
this.apiController = apiController;
|
||||||
|
|
|
@ -13,11 +13,12 @@ namespace AIParkingApplication
|
||||||
private HttpClient httpClient;
|
private HttpClient httpClient;
|
||||||
private bool isHttpClientDisposabled;
|
private bool isHttpClientDisposabled;
|
||||||
|
|
||||||
public ApiController(string baseAddress, int numberOfRetry = 5)
|
public ApiController(string serverIPAddress, int numberOfRetry = 5)
|
||||||
{
|
{
|
||||||
|
|
||||||
httpClient = new HttpClient
|
httpClient = new HttpClient
|
||||||
{
|
{
|
||||||
BaseAddress = new Uri(baseAddress),
|
BaseAddress = new Uri($"http://{serverIPAddress}"),
|
||||||
Timeout = TimeSpan.FromSeconds(5)
|
Timeout = TimeSpan.FromSeconds(5)
|
||||||
};
|
};
|
||||||
isHttpClientDisposabled = false;
|
isHttpClientDisposabled = false;
|
||||||
|
|
2
AIParkingApplication/LoginForm.Designer.cs
generated
2
AIParkingApplication/LoginForm.Designer.cs
generated
|
@ -92,7 +92,7 @@
|
||||||
this.txtServerAddress.Name = "txtServerAddress";
|
this.txtServerAddress.Name = "txtServerAddress";
|
||||||
this.txtServerAddress.Size = new System.Drawing.Size(227, 20);
|
this.txtServerAddress.Size = new System.Drawing.Size(227, 20);
|
||||||
this.txtServerAddress.TabIndex = 1;
|
this.txtServerAddress.TabIndex = 1;
|
||||||
this.txtServerAddress.Text = "http://localhost:80/";
|
this.txtServerAddress.Text = "192.168.1.122:80";
|
||||||
//
|
//
|
||||||
// lblServerAddress
|
// lblServerAddress
|
||||||
//
|
//
|
||||||
|
|
|
@ -23,25 +23,32 @@ namespace AIParkingApplication
|
||||||
|
|
||||||
private async void Login()
|
private async void Login()
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(txtServerAddress.Text))
|
btnLogin.Enabled = false;
|
||||||
|
string serverUrl = txtServerAddress.Text;
|
||||||
|
if (string.IsNullOrEmpty(serverUrl))
|
||||||
{
|
{
|
||||||
lblLoginStatus.Text = "Địa chỉ server không được để trống!";
|
lblLoginStatus.Text = "Địa chỉ server không được để trống! \r\n ví dụ: 192.168.1.2:80";
|
||||||
|
btnLogin.Enabled = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Util.IsUrlValid(txtServerAddress.Text))
|
Util.ParseHostString(serverUrl, out string ipAddress, out int port);
|
||||||
|
|
||||||
|
if (!Util.IsValidIPAddress(ipAddress))
|
||||||
{
|
{
|
||||||
lblLoginStatus.Text = "Địa chỉ server không đúng định dạng: \r\n http://localhost:80 hoặc http://192.168.1.2:80";
|
lblLoginStatus.Text = "Địa chỉ server không đúng định dạng: \r\n ví dụ: 192.168.1.122:80";
|
||||||
|
btnLogin.Enabled = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
apiController = new ApiController(txtServerAddress.Text);
|
apiController = new ApiController(serverUrl);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show($"Cấu máy chủ lỗi - Kiểm tra lại tên máy chủ, kết nối máy chủ! \r\n {ex.Message}", "Lỗi khởi tạo API!", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show($"Cấu máy chủ lỗi - Kiểm tra lại tên máy chủ, kết nối máy chủ! \r\n {ex.Message}", "Lỗi khởi tạo API!", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
btnLogin.Enabled = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,12 +58,14 @@ namespace AIParkingApplication
|
||||||
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
|
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
|
||||||
{
|
{
|
||||||
lblLoginStatus.Text = "Tên tài khoản hoặc mật khẩu không được để trống!";
|
lblLoginStatus.Text = "Tên tài khoản hoặc mật khẩu không được để trống!";
|
||||||
|
btnLogin.Enabled = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var loginResult = await apiController.Login(new LoginModel { Username = username, Password = password });
|
var loginResult = await apiController.Login(new LoginModel { Username = username, Password = password });
|
||||||
if (loginResult.Exception != null)
|
if (loginResult.Exception != null)
|
||||||
{
|
{
|
||||||
|
btnLogin.Enabled = true;
|
||||||
var execeptioMessage = loginResult.Exception.Message;
|
var execeptioMessage = loginResult.Exception.Message;
|
||||||
if (execeptioMessage.Contains("Error converting value"))
|
if (execeptioMessage.Contains("Error converting value"))
|
||||||
{
|
{
|
||||||
|
@ -74,11 +83,13 @@ namespace AIParkingApplication
|
||||||
|
|
||||||
if (loginResult.IsLoginSuccess)
|
if (loginResult.IsLoginSuccess)
|
||||||
{
|
{
|
||||||
new AIParkingApplicationForm(apiController, loginResult.LoginData).Show();
|
new AIParkingApplicationForm(apiController, ipAddress, loginResult.LoginData).Show();
|
||||||
Hide();
|
Hide();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void btnLogin_Click(object sender, EventArgs e)
|
private void btnLogin_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
Login();
|
Login();
|
||||||
|
|
|
@ -154,12 +154,25 @@ namespace AIParkingApplication
|
||||||
return isValidIPAddress;
|
return isValidIPAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsUrlValid(string url)
|
public static void ParseHostString(string hostString, out string hostName, out int port)
|
||||||
{
|
{
|
||||||
|
if (!hostString.Contains(":"))
|
||||||
|
{
|
||||||
|
hostName = hostString;
|
||||||
|
port = 80;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
string pattern = @"^(http|https|ftp|)\://|[a-zA-Z0-9\-\.]+\.[a-zA-Z](:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$";
|
string[] hostParts = hostString.Split(':');
|
||||||
Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
if (hostParts.Length != 2)
|
||||||
return reg.IsMatch(url);
|
{
|
||||||
|
hostName = string.Empty;
|
||||||
|
port = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
hostName = hostParts[0];
|
||||||
|
int.TryParse(hostParts[1], out port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user