From fa752ba1fb49dca9760e82a7afb668dab73a28d8 Mon Sep 17 00:00:00 2001 From: Mahdiyar Ghannad Date: Wed, 9 Aug 2023 18:55:33 +0330 Subject: [PATCH] Support for uniqueidentity --- .../OpenAPI.cs | 90 ++++--------------- .../OpenAPI.nswag.json | 34 +------ ...AuthenticationsMicroservice.Clients.csproj | 2 +- .../Responses/UserResponseContract.cs | 9 +- .../JWTManager.cs | 14 ++- 5 files changed, 29 insertions(+), 120 deletions(-) diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/Connected Services/AuthenticationsGeneratedServices/OpenAPI.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/Connected Services/AuthenticationsGeneratedServices/OpenAPI.cs index 7a4cc52..5bffcec 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/Connected Services/AuthenticationsGeneratedServices/OpenAPI.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/Connected Services/AuthenticationsGeneratedServices/OpenAPI.cs @@ -209,80 +209,6 @@ public virtual async System.Threading.Tasks.TaskSuccess - /// A server side error occurred. - public virtual System.Threading.Tasks.Task TestAsync() - { - return TestAsync(System.Threading.CancellationToken.None); - } - - /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. - /// Success - /// A server side error occurred. - public virtual async System.Threading.Tasks.Task TestAsync(System.Threading.CancellationToken cancellationToken) - { - var urlBuilder_ = new System.Text.StringBuilder(); - urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/Users/Test"); - - var client_ = _httpClient; - var disposeClient_ = false; - try - { - using (var request_ = new System.Net.Http.HttpRequestMessage()) - { - request_.Method = new System.Net.Http.HttpMethod("GET"); - request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("text/plain")); - - PrepareRequest(client_, request_, urlBuilder_); - - var url_ = urlBuilder_.ToString(); - request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute); - - PrepareRequest(client_, request_, url_); - - var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false); - var disposeResponse_ = true; - try - { - var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value); - if (response_.Content != null && response_.Content.Headers != null) - { - foreach (var item_ in response_.Content.Headers) - headers_[item_.Key] = item_.Value; - } - - ProcessResponse(client_, response_); - - var status_ = (int)response_.StatusCode; - if (status_ == 200) - { - var objectResponse_ = await ReadObjectResponseAsync(response_, headers_, cancellationToken).ConfigureAwait(false); - if (objectResponse_.Object == null) - { - throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null); - } - return objectResponse_.Object; - } - else - { - var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false); - throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null); - } - } - finally - { - if (disposeResponse_) - response_.Dispose(); - } - } - } - finally - { - if (disposeClient_) - client_.Dispose(); - } - } - /// Success /// A server side error occurred. public virtual System.Threading.Tasks.Task GetByIdAsync(long? id) @@ -1558,6 +1484,7 @@ protected virtual void RaisePropertyChanged([System.Runtime.CompilerServices.Cal public partial class UserResponseContract : System.ComponentModel.INotifyPropertyChanged { private string _token; + private string _uniqueIdentity; [Newtonsoft.Json.JsonProperty("token", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] public string Token @@ -1574,6 +1501,21 @@ public string Token } } + [Newtonsoft.Json.JsonProperty("uniqueIdentity", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)] + public string UniqueIdentity + { + get { return _uniqueIdentity; } + + set + { + if (_uniqueIdentity != value) + { + _uniqueIdentity = value; + RaisePropertyChanged(); + } + } + } + public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; protected virtual void RaisePropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/Connected Services/AuthenticationsGeneratedServices/OpenAPI.nswag.json b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/Connected Services/AuthenticationsGeneratedServices/OpenAPI.nswag.json index 5d95de8..1e39a5a 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/Connected Services/AuthenticationsGeneratedServices/OpenAPI.nswag.json +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/Connected Services/AuthenticationsGeneratedServices/OpenAPI.nswag.json @@ -103,36 +103,6 @@ } } }, - "/api/Users/Test": { - "get": { - "tags": [ - "Users" - ], - "operationId": "Test", - "responses": { - "200": { - "description": "Success", - "content": { - "text/plain": { - "schema": { - "type": "string" - } - }, - "application/json": { - "schema": { - "type": "string" - } - }, - "text/json": { - "schema": { - "type": "string" - } - } - } - } - } - } - }, "/api/Users/GetById": { "get": { "tags": [ @@ -664,6 +634,10 @@ "token": { "type": "string", "nullable": true + }, + "uniqueIdentity": { + "type": "string", + "nullable": true } } }, diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/EasyMicroservices.AuthenticationsMicroservice.Clients.csproj b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/EasyMicroservices.AuthenticationsMicroservice.Clients.csproj index 144c2f7..7eaac94 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/EasyMicroservices.AuthenticationsMicroservice.Clients.csproj +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Clients/EasyMicroservices.AuthenticationsMicroservice.Clients.csproj @@ -5,7 +5,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.2 + 0.0.0.3 client generated code. EasyMicroservices@gmail.com microservice,auth,authentication,client diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Responses/UserResponseContract.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Responses/UserResponseContract.cs index 882710f..4ac48b6 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Responses/UserResponseContract.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Domain/Contracts/Responses/UserResponseContract.cs @@ -1,13 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using EasyMicroservices.Cores.Interfaces; namespace EasyMicroservices.AuthenticationsMicroservice.Contracts.Responses { - public class UserResponseContract + public class UserResponseContract : IUniqueIdentitySchema { public string Token { get; set; } + public string UniqueIdentity { get; set; } } } diff --git a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Services/JWTManager.cs b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Services/JWTManager.cs index ce66a57..e277547 100644 --- a/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Services/JWTManager.cs +++ b/src/CSharp/EasyMicroservices.AuthenticationsMicroservice.Services/JWTManager.cs @@ -32,14 +32,9 @@ public virtual async Task> Login(UserClaim { string Password = await AuthenticationHelper.HashPassword(cred.Password); - var usersRecords = await _userLogic.GetAll(); - var user = usersRecords.Result.Where(x => x.UserName == cred.UserName && x.Password == Password); - UserContract userData = new(); - - if (!user.Any()) - return (FailedReasonType.AccessDenied, "Username or password is invalid."); - else - userData = user.FirstOrDefault(); + var user = await _userLogic.GetBy(x => x.UserName == cred.UserName && x.Password == Password); + if (!user) + return user.ToContract();//"Username or password is invalid." var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.UTF8.GetBytes(_config.GetValue("JWT:Key")); @@ -56,7 +51,8 @@ public virtual async Task> Login(UserClaim return new UserResponseContract { - Token = tokenString + Token = tokenString, + UniqueIdentity = user.Result.UniqueIdentity }; }