Bug in nopcommerce 4.60.2 (user is not logged in after registring)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 year ago
There is a bug that doesn't log in the user after successfully registering when registration method is set to 'standard account creation'. My guess is that somehow the code is running too quickly.
After a bit of investigation, I found out that the problem is in
Libraries\Nop.Services\Authentication\CookieAuthenticationService.cs at this line await _httpContextAccessor.HttpContext.SignInAsync(NopAuthenticationDefaults.AuthenticationScheme, userPrincipal, authenticationProperties);


How to reproduce
create a few accounts (sometimes it works fine), and make sure you have no breakpoints.

Running webapp with breakpoints before
var authenticationProperties = new AuthenticationProperties 
or in other register methods, everything works fine again. The temporary workaround: I use await Task.Delay(1000); before var authenticationProperties

public virtual async Task SignInAsync(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);

            await Task.Delay(1000);

            //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 year ago
Thanks a lot for report. We'll check it soon - https://github.com/nopSolutions/nopCommerce/issues/6670
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.