problem with override method in dependency registrar

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 ano atrás
Hello everyone,
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;
        }
    }
}
1 ano atrás
override like this
  public partial class CustomCookieAuthenticationService : CookieAuthenticationService
and register like this
builder.RegisterType<CustomCookieAuthenticationService >().As<IAuthenticationService>().InstancePerLifetimeScope();
and give  big Order to your DependencyRegistrar
public int Order => int.MaxValue;
1 ano atrás
Thanks, It Work !!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.