Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Minor changes ApiAuthenticationService.cs
support8aa7e655a8f5
diff --git a/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs b/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.csindex ab8fad61f..7cc8cc8bf 100644--- a/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs+++ b/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs@@ -12,47 +12,44 @@ namespace Grand.Business.Authentication.Services{public class ApiAuthenticationService : IApiAuthenticationService{- private readonly IHttpContextAccessor _httpContextAccessor;private readonly ICustomerService _customerService;private readonly IGroupService _groupService;private Customer _cachedCustomer;public ApiAuthenticationService(- IHttpContextAccessor httpContextAccessor,ICustomerService customerService,IGroupService groupService){- _httpContextAccessor = httpContextAccessor;_customerService = customerService;_groupService = groupService;}- public virtual async Task<Customer> GetAuthenticatedCustomer()+ public virtual async Task<Customer> GetAuthenticatedCustomer(HttpContext httpContext){//whether there is a cached customerif (_cachedCustomer != null)return _cachedCustomer;Customer customer = null;- if (_httpContextAccessor.HttpContext == null) return null;+ if (httpContext == null) return null;//try to get authenticated user identity- string authHeader = _httpContextAccessor.HttpContext.Request.Headers[HeaderNames.Authorization];+ string authHeader = httpContext.Request.Headers[HeaderNames.Authorization];if (string.IsNullOrEmpty(authHeader) || !authHeader.StartsWith(JwtBearerDefaults.AuthenticationScheme))return null;- if (_httpContextAccessor.HttpContext.Request.Path.Value != null- && !_httpContextAccessor.HttpContext.Request.Path.Value.StartsWith("/odata"))+ if (httpContext.Request.Path.Value != null+ && !httpContext.Request.Path.Value.StartsWith("/odata")){- customer = await ApiCustomer();+ customer = await ApiCustomer(httpContext);if (customer == null) return null;_cachedCustomer = customer;return _cachedCustomer;}var authenticateResult =- await _httpContextAccessor.HttpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);+ await httpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);if (!authenticateResult.Succeeded)return null;@@ -71,17 +68,16 @@ namespace Grand.Business.Authentication.Servicesreturn _cachedCustomer;}- private async Task<Customer> ApiCustomer()+ private async Task<Customer> ApiCustomer(HttpContext httpContext){Customer customer = null;- if (_httpContextAccessor.HttpContext == null) return null;- var authResult = await _httpContextAccessor.HttpContext.AuthenticateAsync(FrontendAPIConfig.AuthenticationScheme);+ var authResult = await httpContext.AuthenticateAsync(FrontendAPIConfig.AuthenticationScheme);if (!authResult.Succeeded){- _httpContextAccessor.HttpContext.Response.StatusCode = 400;- _httpContextAccessor.HttpContext.Response.ContentType = "text/plain";+ httpContext.Response.StatusCode = 400;+ httpContext.Response.ContentType = "text/plain";if (authResult.Failure != null)- await _httpContextAccessor.HttpContext.Response.WriteAsync(authResult.Failure.Message);+ await httpContext.Response.WriteAsync(authResult.Failure.Message);return await _customerService.GetCustomerBySystemName(SystemCustomerNames.Anonymous);}var email = authResult.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "Email")?.Value;diff --git a/src/Business/Grand.Business.Core/Interfaces/Authentication/IApiAuthenticationService.cs b/src/Business/Grand.Business.Core/Interfaces/Authentication/IApiAuthenticationService.csindex e218e33f1..7237083ff 100644--- a/src/Business/Grand.Business.Core/Interfaces/Authentication/IApiAuthenticationService.cs+++ b/src/Business/Grand.Business.Core/Interfaces/Authentication/IApiAuthenticationService.cs@@ -1,4 +1,5 @@using Grand.Domain.Customers;+using Microsoft.AspNetCore.Http;namespace Grand.Business.Core.Interfaces.Authentication{@@ -8,6 +9,6 @@ namespace Grand.Business.Core.Interfaces.Authentication/// Get authenticated customer/// </summary>/// <returns>Customer</returns>- Task<Customer> GetAuthenticatedCustomer();+ Task<Customer> GetAuthenticatedCustomer(HttpContext httpContext);}}diff --git a/src/Tests/Grand.Business.Authentication.Tests/Services/ApiAuthenticationServiceTests.cs b/src/Tests/Grand.Business.Authentication.Tests/Services/ApiAuthenticationServiceTests.csindex aa312d205..a92ab274b 100644--- a/src/Tests/Grand.Business.Authentication.Tests/Services/ApiAuthenticationServiceTests.cs+++ b/src/Tests/Grand.Business.Authentication.Tests/Services/ApiAuthenticationServiceTests.cs@@ -18,7 +18,6 @@ namespace Grand.Business.Authentication.Tests.Servicesprivate Mock<IHttpContextAccessor> _httpContextAccessorMoc;private Mock<ICustomerService> _customerService;private Mock<IUserApiService> _userApiServiceMock;- private Mock<HttpContext> _httpContextMock;private Mock<IGroupService> _groupService;private IApiAuthenticationService _authService;private IJwtBearerAuthenticationService _jwtBearerAuthenticationService;@@ -30,8 +29,7 @@ namespace Grand.Business.Authentication.Tests.Services_customerService = new Mock<ICustomerService>();_userApiServiceMock = new Mock<IUserApiService>();_groupService = new Mock<IGroupService>();- _httpContextMock = new Mock<HttpContext>();- _authService = new ApiAuthenticationService(_httpContextAccessorMoc.Object, _customerService.Object, _groupService.Object);+ _authService = new ApiAuthenticationService(_customerService.Object, _groupService.Object);_jwtBearerAuthenticationService = new JwtBearerAuthenticationService(_customerService.Object, _userApiServiceMock.Object);}@@ -163,7 +161,7 @@ namespace Grand.Business.Authentication.Tests.ServiceshttpContext.Setup(c => c.Request).Returns(req.Object);_httpContextAccessorMoc.Setup(c => c.HttpContext).Returns(httpContext.Object);_customerService.Setup(c => c.GetCustomerBySystemName(It.IsAny<string>())).Returns(() => Task.FromResult(customer));- var result = await _authService.GetAuthenticatedCustomer();+ var result = await _authService.GetAuthenticatedCustomer(httpContext.Object);Assert.IsNull(result);}