LoginForm - Check IP Address Input.

This commit is contained in:
DucDangAnh 2020-07-13 14:51:44 +07:00
parent 728f65cd90
commit 74855b6467
5 changed files with 39 additions and 14 deletions

View File

@ -20,7 +20,7 @@ namespace AIParkingApplication
private EngineApiController engineApiController;
private string doorAccessControlDeviceIP;
public AIParkingApplicationForm(ApiController apiController, Config configOnWeb)
public AIParkingApplicationForm(ApiController apiController, string serverIPAddress, Config configOnWeb)
{
InitializeComponent();
this.apiController = apiController;

View File

@ -13,11 +13,12 @@ namespace AIParkingApplication
private HttpClient httpClient;
private bool isHttpClientDisposabled;
public ApiController(string baseAddress, int numberOfRetry = 5)
public ApiController(string serverIPAddress, int numberOfRetry = 5)
{
httpClient = new HttpClient
{
BaseAddress = new Uri(baseAddress),
BaseAddress = new Uri($"http://{serverIPAddress}"),
Timeout = TimeSpan.FromSeconds(5)
};
isHttpClientDisposabled = false;

View File

@ -92,7 +92,7 @@
this.txtServerAddress.Name = "txtServerAddress";
this.txtServerAddress.Size = new System.Drawing.Size(227, 20);
this.txtServerAddress.TabIndex = 1;
this.txtServerAddress.Text = "http://localhost:80/";
this.txtServerAddress.Text = "192.168.1.122:80";
//
// lblServerAddress
//

View File

@ -23,25 +23,32 @@ namespace AIParkingApplication
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;
}
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;
}
try
{
apiController = new ApiController(txtServerAddress.Text);
apiController = new ApiController(serverUrl);
}
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);
btnLogin.Enabled = true;
return;
}
@ -51,12 +58,14 @@ namespace AIParkingApplication
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!";
btnLogin.Enabled = true;
return;
}
var loginResult = await apiController.Login(new LoginModel { Username = username, Password = password });
if (loginResult.Exception != null)
{
btnLogin.Enabled = true;
var execeptioMessage = loginResult.Exception.Message;
if (execeptioMessage.Contains("Error converting value"))
{
@ -74,11 +83,13 @@ namespace AIParkingApplication
if (loginResult.IsLoginSuccess)
{
new AIParkingApplicationForm(apiController, loginResult.LoginData).Show();
new AIParkingApplicationForm(apiController, ipAddress, loginResult.LoginData).Show();
Hide();
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
Login();

View File

@ -154,12 +154,25 @@ namespace AIParkingApplication
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]$";
Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return reg.IsMatch(url);
string[] hostParts = hostString.Split(':');
if (hostParts.Length != 2)
{
hostName = string.Empty;
port = 0;
return;
}
hostName = hostParts[0];
int.TryParse(hostParts[1], out port);
}
}