Commit 54ec1d30 authored by tungpt55's avatar tungpt55

Fig bug login too many

parent 4249291d

Too many changes to show.

To preserve performance only 1000 of 1000+ files are displayed.


using Accw;
using Microsoft.Extensions.Configuration;
using Serilog;
using System;
using System.Reflection;
using System.Threading.Tasks;
using System.Web.Http;
using WPCommSharpClient.FormRequests;
namespace WPCommSharpClient
{
public class ApiController : System.Web.Http.ApiController
{
private readonly MTSCBServerClass _server;
private readonly WPCallbackClient _client;
public ApiController(MTSCBServerClass server)
{
_server = server ?? throw new ArgumentNullException(nameof(server));
_client = new WPCallbackClient();
}
[HttpPost]
[Route("api/login")]
public async Task<IHttpActionResult> Login([FromBody] FormLoginRequest loginRequest)
{
Log.Debug("Calling login....");
if (loginRequest == null)
{
return Json(ResponseResult<object>.ErrorResult("Invalid login request."));
}
try
{
bool isConnected = string.IsNullOrWhiteSpace(loginRequest.Domain)
? _server.InitServer(_client, 3, loginRequest.UserName, loginRequest.Password, 1)
: _server.InitServer2(_client, 3, loginRequest.UserName, loginRequest.Password, loginRequest.Domain, 1);
Log.Information("Login {Status}", isConnected ? "successful" : "failed");
//var listDevice = _server.ListConnectedDevices();
//Log.Debug(listDevice.ToString());
if (isConnected)
{
return Json(ResponseResult<object>.SuccessResult(null, "Login successful."));
}
else
{
return Json(ResponseResult<object>.ErrorResult("Login failed."));
}
}
catch (Exception ex)
{
Log.Error(ex, "Login failed.");
return Json(ResponseResult<object>.ErrorResult("An error occurred during login."));
}
}
[HttpGet]
[Route("api/logout")]
public async Task<IHttpActionResult> logout()
{
Log.Debug("Calling logout....");
int isConnected = 0;
_server.IsConnected(out isConnected);
if(isConnected > 0)
{
bool isLogout = _server.DoneServer(_client);
if( isLogout)
{
Log.Debug("Logout successfully.");
return Json(ResponseResult<object>.SuccessResult(null, "Logout successfully"));
}
else
{
return Json(ResponseResult<object>.ErrorResult("Logout fail"));
}
}
return Json(ResponseResult<object>.ErrorResult("Server not login!!!"));
}
[HttpPost]
[Route("api/unLock")]
public async Task<IHttpActionResult> EntryUnLockById([FromBody] FormLockRequest formLockRequest)
{
DateTime startCallAPI = DateTime.Now;
Log.Debug("Calling unlock ....");
// Đọc cấu hình từ appsettings.json // 1 thu vien doc file json
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
var login = configuration.GetSection("login");
string username = login["username"];
string password = login["password"];
Log.Debug("Username: " + username);
Log.Debug("Password: " + password);
if(string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
Log.Error("Not found username or password!");
return Json(ResponseResult<Object>.ErrorResult("Not found username or password!"));
}
// goị tới login của server
// _client: mặc định với _client là đối tượng có sẵn
// 3 : tức nhận cả Alarm và Event( 1: Alarm, 2: Event )
// username
// password
// 1: id của user đăng nhâp(ở đây đăng nhập với user mặc định ban đầu của winPak là 1)
bool isConnected = _server.InitServer(_client, 3, username, password, 1);
if(isConnected)
{
if (formLockRequest?.HID == null)
{
Log.Error("Not found HID!!!");
return Json(ResponseResult<String>.ErrorResult("HID is required."));
}
try
{
// Hàm nhận vào 1 giá trị là Hardware ID
_server.EntryPointUnLockByHID(formLockRequest.HID.Value);
Log.Debug("UnLock successful for HID={HID}", formLockRequest.HID);
TimeSpan totalTime = (DateTime.Now - startCallAPI);
Log.Debug($"Total time: {totalTime.TotalMilliseconds} ms");
return Json(ResponseResult<String>.SuccessResult(null, "UnLock successful."));
}
catch (Exception ex)
{
Log.Error(ex, "Failed to unlock entry.");
return Json(ResponseResult<String>.ErrorResult("An error occurred during unlocking."));
}
}else
{
Log.Error("Error Login with initServer");
return Json(ResponseResult<object>.ErrorResult("An error occurred during login."));
}
}
[HttpGet]
[Route("api/getStatus")]
public async Task<IHttpActionResult> GetConnectToServer()
{
if (_server == null)
{
Log.Error("Server instance is null");
return Json(ResponseResult<object>.ErrorResult("Server instance is null."));
}
try
{
_server.IsConnected(out int isConnected);
Log.Information("Connection status: {Status}", isConnected == 1 ? "Connected" : "Disconnected");
return isConnected == 1
? Json(ResponseResult<object>.SuccessResult(null, "Server is connected."))
: Json(ResponseResult<object>.ErrorResult("Server is not connected."));
}
catch (Exception ex)
{
Log.Error(ex, "Failed to check connection status.");
return Json(ResponseResult<object>.ErrorResult("An error occurred while checking the server status."));
}
}
}
}
using System.Web.Http;
using Accw;
using Owin;
using SimpleInjector;
using SimpleInjector.Integration.WebApi;
using SimpleInjector.Lifestyles;
namespace WPCommSharpClient
{
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
var container = ConfigureDependencyInjection();
HttpConfiguration config = ConfigureWebApi(container);
appBuilder.UseWebApi(config);
}
private Container ConfigureDependencyInjection()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
// Suwr dungj singleton xuyen suot vong doi ung dung
container.Register<MTSCBServerClass>(Lifestyle.Singleton);
container.Register<ApiController>(Lifestyle.Scoped);
container.Verify();
return container;
}
private HttpConfiguration ConfigureWebApi(Container container)
{
var config = new HttpConfiguration
{
DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container)
};
// Thiết lập DependencyResolver để sử dụng Simple Injector
config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// config.Filters.Add(new AuthorizeAttribute()); // Thêm kiểm tra xác thực
return config;
}
}
}
<Project InitialTargets="NETStandardCompatError_Microsoft_Bcl_AsyncInterfaces_net8_0">
<Target Name="NETStandardCompatError_Microsoft_Bcl_AsyncInterfaces_net8_0"
Condition="'$(SuppressTfmSupportBuildWarnings)' == ''">
<Warning Text="Microsoft.Bcl.AsyncInterfaces 9.0.0 doesn't support $(TargetFramework) and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set &lt;SuppressTfmSupportBuildWarnings&gt;true&lt;/SuppressTfmSupportBuildWarnings&gt; in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk." />
</Target>
</Project>
<Project InitialTargets="NETStandardCompatError_Microsoft_Extensions_FileSystemGlobbing_net462">
<Target Name="NETStandardCompatError_Microsoft_Extensions_FileSystemGlobbing_net462"
Condition="'$(SuppressTfmSupportBuildWarnings)' == ''">
<Warning Text="Microsoft.Extensions.FileSystemGlobbing 9.0.0 doesn't support $(TargetFramework) and has not been tested with it. Consider upgrading your TargetFramework to net462 or later. You may also set &lt;SuppressTfmSupportBuildWarnings&gt;true&lt;/SuppressTfmSupportBuildWarnings&gt; in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk." />
</Target>
</Project>
<Project InitialTargets="NETStandardCompatError_Microsoft_Extensions_FileSystemGlobbing_net8_0">
<Target Name="NETStandardCompatError_Microsoft_Extensions_FileSystemGlobbing_net8_0"
Condition="'$(SuppressTfmSupportBuildWarnings)' == ''">
<Warning Text="Microsoft.Extensions.FileSystemGlobbing 9.0.0 doesn't support $(TargetFramework) and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set &lt;SuppressTfmSupportBuildWarnings&gt;true&lt;/SuppressTfmSupportBuildWarnings&gt; in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk." />
</Target>
</Project>
<Project InitialTargets="NETStandardCompatError_Microsoft_Extensions_Primitives_net462">
<Target Name="NETStandardCompatError_Microsoft_Extensions_Primitives_net462"
Condition="'$(SuppressTfmSupportBuildWarnings)' == ''">
<Warning Text="Microsoft.Extensions.Primitives 9.0.0 doesn't support $(TargetFramework) and has not been tested with it. Consider upgrading your TargetFramework to net462 or later. You may also set &lt;SuppressTfmSupportBuildWarnings&gt;true&lt;/SuppressTfmSupportBuildWarnings&gt; in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk." />
</Target>
</Project>
<Project InitialTargets="NETStandardCompatError_Microsoft_Extensions_Primitives_net8_0">
<Target Name="NETStandardCompatError_Microsoft_Extensions_Primitives_net8_0"
Condition="'$(SuppressTfmSupportBuildWarnings)' == ''">
<Warning Text="Microsoft.Extensions.Primitives 9.0.0 doesn't support $(TargetFramework) and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set &lt;SuppressTfmSupportBuildWarnings&gt;true&lt;/SuppressTfmSupportBuildWarnings&gt; in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk." />
</Target>
</Project>
<Project InitialTargets="NETStandardCompatError_System_Diagnostics_DiagnosticSource_net462">
<Target Name="NETStandardCompatError_System_Diagnostics_DiagnosticSource_net462"
Condition="'$(SuppressTfmSupportBuildWarnings)' == ''">
<Warning Text="System.Diagnostics.DiagnosticSource 8.0.1 doesn't support $(TargetFramework) and has not been tested with it. Consider upgrading your TargetFramework to net462 or later. You may also set &lt;SuppressTfmSupportBuildWarnings&gt;true&lt;/SuppressTfmSupportBuildWarnings&gt; in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk." />
</Target>
</Project>
<Project InitialTargets="NETStandardCompatError_System_Diagnostics_DiagnosticSource_net6_0">
<Target Name="NETStandardCompatError_System_Diagnostics_DiagnosticSource_net6_0"
Condition="'$(SuppressTfmSupportBuildWarnings)' == ''">
<Warning Text="System.Diagnostics.DiagnosticSource 8.0.1 doesn't support $(TargetFramework) and has not been tested with it. Consider upgrading your TargetFramework to net6.0 or later. You may also set &lt;SuppressTfmSupportBuildWarnings&gt;true&lt;/SuppressTfmSupportBuildWarnings&gt; in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk." />
</Target>
</Project>
<Project InitialTargets="NETStandardCompatError_System_Runtime_CompilerServices_Unsafe_netcoreapp3_1">
<Target Name="NETStandardCompatError_System_Runtime_CompilerServices_Unsafe_netcoreapp3_1"
Condition="'$(SuppressTfmSupportBuildWarnings)' == ''">
<Error Text="System.Runtime.CompilerServices.Unsafe doesn't support $(TargetFramework). Consider updating your TargetFramework to netcoreapp3.1 or later." />
</Target>
</Project>
<?xml version="1.0" encoding="utf-8"?><doc>
<assembly>
<name>System.Threading.Tasks.Extensions</name>
</assembly>
<members>
<member name="T:System.Runtime.CompilerServices.ValueTaskAwaiter`1">
<typeparam name="TResult"></typeparam>
</member>
<member name="M:System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult">
<returns></returns>
</member>
<member name="P:System.Runtime.CompilerServices.ValueTaskAwaiter`1.IsCompleted">
<returns></returns>
</member>
<member name="M:System.Runtime.CompilerServices.ValueTaskAwaiter`1.OnCompleted(System.Action)">
<param name="continuation"></param>
</member>
<member name="M:System.Runtime.CompilerServices.ValueTaskAwaiter`1.UnsafeOnCompleted(System.Action)">
<param name="continuation"></param>
</member>
<member name="T:System.Threading.Tasks.ValueTask`1">
<summary>Provides a value type that wraps a <see cref="Task{TResult}"></see> and a <typeparamref name="TResult">TResult</typeparamref>, only one of which is used.</summary>
<typeparam name="TResult">The result.</typeparam>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.#ctor(System.Threading.Tasks.Task{`0})">
<summary>Initializes a new instance of the <see cref="ValueTask{TResult}"></see> class using the supplied task that represents the operation.</summary>
<param name="task">The task.</param>
<exception cref="T:System.ArgumentNullException">The <paramref name="task">task</paramref> argument is null.</exception>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.#ctor(`0)">
<summary>Initializes a new instance of the <see cref="ValueTask{TResult}"></see> class using the supplied result of a successful operation.</summary>
<param name="result">The result.</param>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.AsTask">
<summary>Retrieves a <see cref="Task{TResult}"></see> object that represents this <see cref="ValueTask{TResult}"></see>.</summary>
<returns>The <see cref="Task{TResult}"></see> object that is wrapped in this <see cref="ValueTask{TResult}"></see> if one exists, or a new <see cref="Task{TResult}"></see> object that represents the result.</returns>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.ConfigureAwait(System.Boolean)">
<summary>Configures an awaiter for this value.</summary>
<param name="continueOnCapturedContext">true to attempt to marshal the continuation back to the captured context; otherwise, false.</param>
<returns>The configured awaiter.</returns>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.CreateAsyncMethodBuilder">
<summary>Creates a method builder for use with an async method.</summary>
<returns>The created builder.</returns>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.Equals(System.Object)">
<summary>Determines whether the specified object is equal to the current object.</summary>
<param name="obj">The object to compare with the current object.</param>
<returns>true if the specified object is equal to the current object; otherwise, false.</returns>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.Equals(System.Threading.Tasks.ValueTask{`0})">
<summary>Determines whether the specified <see cref="ValueTask{TResult}"></see> object is equal to the current <see cref="ValueTask{TResult}"></see> object.</summary>
<param name="other">The object to compare with the current object.</param>
<returns>true if the specified object is equal to the current object; otherwise, false.</returns>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.GetAwaiter">
<summary>Creates an awaiter for this value.</summary>
<returns>The awaiter.</returns>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.GetHashCode">
<summary>Returns the hash code for this instance.</summary>
<returns>The hash code for the current object.</returns>
</member>
<member name="P:System.Threading.Tasks.ValueTask`1.IsCanceled">
<summary>Gets a value that indicates whether this object represents a canceled operation.</summary>
<returns>true if this object represents a canceled operation; otherwise, false.</returns>
</member>
<member name="P:System.Threading.Tasks.ValueTask`1.IsCompleted">
<summary>Gets a value that indicates whether this object represents a completed operation.</summary>
<returns>true if this object represents a completed operation; otherwise, false.</returns>
</member>
<member name="P:System.Threading.Tasks.ValueTask`1.IsCompletedSuccessfully">
<summary>Gets a value that indicates whether this object represents a successfully completed operation.</summary>
<returns>true if this object represents a successfully completed operation; otherwise, false.</returns>
</member>
<member name="P:System.Threading.Tasks.ValueTask`1.IsFaulted">
<summary>Gets a value that indicates whether this object represents a failed operation.</summary>
<returns>true if this object represents a failed operation; otherwise, false.</returns>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.op_Equality(System.Threading.Tasks.ValueTask{`0},System.Threading.Tasks.ValueTask{`0})">
<summary>Compares two values for equality.</summary>
<param name="left">The first value to compare.</param>
<param name="right">The second value to compare.</param>
<returns>true if the two <see cref="ValueTask{TResult}"></see> values are equal; otherwise, false.</returns>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.op_Inequality(System.Threading.Tasks.ValueTask{`0},System.Threading.Tasks.ValueTask{`0})">
<summary>Determines whether two <see cref="ValueTask{TResult}"></see> values are unequal.</summary>
<param name="left">The first value to compare.</param>
<param name="right">The seconed value to compare.</param>
<returns>true if the two <see cref="ValueTask{TResult}"></see> values are not equal; otherwise, false.</returns>
</member>
<member name="P:System.Threading.Tasks.ValueTask`1.Result">
<summary>Gets the result.</summary>
<returns>The result.</returns>
</member>
<member name="M:System.Threading.Tasks.ValueTask`1.ToString">
<summary>Returns a string that represents the current object.</summary>
<returns>A string that represents the current object.</returns>
</member>
<member name="T:System.Runtime.CompilerServices.AsyncMethodBuilderAttribute">
</member>
<member name="M:System.Runtime.CompilerServices.AsyncMethodBuilderAttribute.#ctor(System.Type)">
<param name="builderType"></param>
</member>
<member name="P:System.Runtime.CompilerServices.AsyncMethodBuilderAttribute.BuilderType">
<returns></returns>
</member>
<member name="T:System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1">
<typeparam name="TResult"></typeparam>
</member>
<member name="M:System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1.AwaitOnCompleted``2(``0@,``1@)">
<param name="awaiter"></param>
<param name="stateMachine"></param>
<typeparam name="TAwaiter"></typeparam>
<typeparam name="TStateMachine"></typeparam>
</member>
<member name="M:System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1.AwaitUnsafeOnCompleted``2(``0@,``1@)">
<param name="awaiter"></param>
<param name="stateMachine"></param>
<typeparam name="TAwaiter"></typeparam>
<typeparam name="TStateMachine"></typeparam>
</member>
<member name="M:System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1.Create">
<returns></returns>
</member>
<member name="M:System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1.SetException(System.Exception)">
<param name="exception"></param>
</member>
<member name="M:System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1.SetResult(`0)">
<param name="result"></param>
</member>
<member name="M:System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1.SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine)">
<param name="stateMachine"></param>
</member>
<member name="M:System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1.Start``1(``0@)">
<param name="stateMachine"></param>
<typeparam name="TStateMachine"></typeparam>
</member>
<member name="P:System.Runtime.CompilerServices.AsyncValueTaskMethodBuilder`1.Task">
<returns></returns>
</member>
<member name="T:System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter">
<typeparam name="TResult"></typeparam>
</member>
<member name="M:System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult">
<returns></returns>
</member>
<member name="P:System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.IsCompleted">
<returns></returns>
</member>
<member name="M:System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.OnCompleted(System.Action)">
<param name="continuation"></param>
</member>
<member name="M:System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.UnsafeOnCompleted(System.Action)">
<param name="continuation"></param>
</member>
<member name="T:System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1">
<typeparam name="TResult"></typeparam>
</member>
<member name="M:System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.GetAwaiter">
<returns></returns>
</member>
</members>
</doc>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment