I'm developping a plugin and i would like to override the signIn method in CookieAuthenticationService. I map my new class in DependencyRegistrar like this:
builder.RegisterType<CustomCookieAuthenticationService>().As<CookieAuthenticationService>().InstancePerLifetimeScope();
I'm on Nop 4.3 and the probleme is that NopCommerce does'nt use my custom class. Do you know why ?
Thanks for answers
that's my custom CookieAuthentificationService:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Nop.Core.Domain.Customers;
using Nop.Services.Authentication;
using Nop.Services.Customers;
using IAuthenticationService = Nop.Services.Authentication.IAuthenticationService;
namespace Nop.Plugin.ExternalAuth.ParagonSSO
{
public partial class CustomCookieAuthenticationService : CookieAuthenticationService, IAuthenticationService
{
private readonly CustomerSettings _customerSettings;
private readonly ICustomerService _customerService;
private readonly IHttpContextAccessor _httpContextAccessor;
private Customer _cachedCustomer;
public CustomCookieAuthenticationService(CustomerSettings customerSettings, ICustomerService customerService, IHttpContextAccessor httpContextAccessor) : base(customerSettings, customerService, httpContextAccessor)
{
_customerSettings = customerSettings;
_customerService = customerService;
_httpContextAccessor = httpContextAccessor;
}
public override async void SignIn(Customer customer, bool isPersistent)
{
if (customer == null)
throw new ArgumentNullException(nameof(customer));
//create claims for customer's username and email
var claims = new List<Claim>();
if (!string.IsNullOrEmpty(customer.Username))
claims.Add(new Claim(ClaimTypes.Name, customer.Username, ClaimValueTypes.String, NopAuthenticationDefaults.ClaimsIssuer));
if (!string.IsNullOrEmpty(customer.Email))
claims.Add(new Claim(ClaimTypes.Email, customer.Email, ClaimValueTypes.Email, NopAuthenticationDefaults.ClaimsIssuer));
//create principal for the current authentication scheme
var userIdentity = new ClaimsIdentity(claims, NopAuthenticationDefaults.AuthenticationScheme);
var userPrincipal = new ClaimsPrincipal(userIdentity);
//set value indicating whether session is persisted and the time at which the authentication was issued
var authenticationProperties = new AuthenticationProperties
{
IsPersistent = isPersistent,
IssuedUtc = DateTime.UtcNow
};
//sign in
await _httpContextAccessor.HttpContext.SignInAsync(NopAuthenticationDefaults.AuthenticationScheme, userPrincipal, authenticationProperties);
//cache authenticated customer
_cachedCustomer = customer;
}
}
}