示例#1
0
        public virtual void AssociateExternalAccountWithUser(Customer customer, OpenAuthenticationParameters parameters)
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }
            string email = null;

            if (parameters.UserClaims != null)
            {
                foreach (var userClaim in parameters.UserClaims.Where(x => x.Contact != null &&
                                                                      !String.IsNullOrEmpty(x.Contact.Email)))
                {
                    email = userClaim.Contact.Email;
                    break;
                }
            }
            var externalAuthenticationRecord = new ExternalAuthenticationRecord
            {
                CustomerId = customer.Id,
                Email      = email,
                ExternalDisplayIdentifier = parameters.ExternalDisplayIdentifier,
                ExternalIdentifier        = parameters.ExternalIdentifier,
                OAuthToken         = parameters.OAuthToken,
                OAuthAccessToken   = parameters.OAuthAccessToken,
                ProviderSystemName = parameters.ProviderSystemName,
            };

            _externalAuthenticationRecordRepository.Insert(externalAuthenticationRecord);
        }
 public RegistrationDetails(OpenAuthenticationParameters parameters)
     : this()
 {
     if (parameters.UserClaims != null)
     {
         foreach (var claim in parameters.UserClaims)
         {
             if (string.IsNullOrEmpty(EmailAddress))
             {
                 if (claim.Contact != null)
                 {
                     EmailAddress = claim.Contact.Email;
                     UserName     = claim.Contact.Email;
                 }
             }
             if (string.IsNullOrEmpty(FirstName))
             {
                 if (claim.Name != null)
                 {
                     FirstName = claim.Name.First;
                 }
             }
             if (string.IsNullOrEmpty(LastName))
             {
                 if (claim.Name != null)
                 {
                     LastName = claim.Name.Last;
                 }
             }
         }
     }
 }
示例#3
0
        public Customer GetUser(OpenAuthenticationParameters parameters)
        {
            var record = _externalAuthenticationRecordRepository.Table
                         .FirstOrDefault(o => o.ExternalDisplayIdentifier == parameters.ExternalDisplayIdentifier &&
                                         o.ProviderSystemName == parameters.ProviderSystemName);

            if (record != null)
            {
                return(_customerService.GetCustomerById(record.CustomerId));
            }
            return(null);
        }
示例#4
0
 public bool AccountExists(OpenAuthenticationParameters parameters)
 {
     throw new NotImplementedException();
 }
示例#5
0
 public void RemoveAssociation(OpenAuthenticationParameters parameters)
 {
     throw new NotImplementedException();
 }
示例#6
0
        public AuthorizationResult Authorize(OpenAuthenticationParameters parameters)
        {
            var userFound    = _openAuthenticationService.GetUser(parameters);
            var userLoggedIn = _workContext.CurrentCustomer.IsRegistered() ? _workContext.CurrentCustomer : null;

            if (AccountAlreadyExists(userFound, userLoggedIn))
            {
                if (AccountIsAssignedToLoggedOnAccount(userFound, userLoggedIn))
                {
                    return(new AuthorizationResult(OpenAuthenticationStatus.AutoRegisteredStandard));
                }
                var result = new AuthorizationResult(OpenAuthenticationStatus.Error);
                result.AddError("Account is already assigned");
                return(result);
            }
            if (AccountDoesNotExistAndUserIsNotLoggedOn(userFound, userLoggedIn))
            {
                ExternalAuthorizerHelper.StoreParametersForRoundTrip(parameters);
                if (AutoRegistrationIsEnabled())
                {
                    #region 注册用户
                    var  currentCustomer = _workContext.CurrentCustomer;
                    var  details         = new RegistrationDetails(parameters);
                    var  randomPassword  = CommonHelper.GenerateRandomDigitCode(20);
                    bool isApproved      = (_customerSettings.UserRegistrationType == UserRegistrationType.Standard) ||
                                           (_customerSettings.UserRegistrationType == UserRegistrationType.EmailValidation &&
                                            !_externalAuthenticationSettings.RequireEmailValidation);
                    var registrationRequest = new CustomerRegistrationRequest(currentCustomer,
                                                                              details.EmailAddress, details.EmailAddress,
                                                                              randomPassword, PasswordFormat.Clear,
                                                                              isApproved);
                    var registrationResult = _customerRegistrationService.RegisterCustomer(registrationRequest);
                    if (registrationResult.Success)
                    {
                        userFound = currentCustomer;
                        _openAuthenticationService.AssociateExternalAccountWithUser(currentCustomer, parameters);
                        ExternalAuthorizerHelper.RemoveParameters();
                        if (isApproved)
                        {
                            _authenticationService.SignIn(userFound ?? userLoggedIn, false);
                        }
                        //_eventPublisher.Publish(new CustomerRegisteredEvent(currentCustomer));
                        if (isApproved)
                        {
                            return(new AuthorizationResult(OpenAuthenticationStatus.AutoRegisteredStandard));
                        }
                        else if (_customerSettings.UserRegistrationType == UserRegistrationType.EmailValidation)
                        {
                            return(new AuthorizationResult(OpenAuthenticationStatus.AutoRegisteredEmailValidation));
                        }
                        else if (_customerSettings.UserRegistrationType == UserRegistrationType.AdminApproval)
                        {
                            //result
                            return(new AuthorizationResult(OpenAuthenticationStatus.AutoRegisteredAdminApproval));
                        }
                    }
                    else
                    {
                        ExternalAuthorizerHelper.RemoveParameters();

                        var result = new AuthorizationResult(OpenAuthenticationStatus.Error);
                        foreach (var error in registrationResult.Errors)
                        {
                            result.AddError(string.Format(error));
                        }
                        return(result);
                    }
                    #endregion
                }
                else if (RegistrationIsEnabled())
                {
                    return(new AuthorizationResult(OpenAuthenticationStatus.AssociateOnLogon));
                }
                else
                {
                    ExternalAuthorizerHelper.RemoveParameters();
                    var result = new AuthorizationResult(OpenAuthenticationStatus.Error);
                    result.AddError("Registration is disabled");
                    return(result);
                }
            }
            if (userFound == null)
            {
                _openAuthenticationService.AssociateExternalAccountWithUser(userLoggedIn, parameters);
            }
            _authenticationService.SignIn(userFound ?? userLoggedIn, false);
            //发布事件
            _eventPublisher.Publish(new CustomerLoggedinEvent(userFound ?? userLoggedIn));
            //日志
            _customerActivityService.InsertActivity("PublicStore.Login", "登录",
                                                    userFound ?? userLoggedIn);
            return(new AuthorizationResult(OpenAuthenticationStatus.Authenticated));
        }
示例#7
0
        public static void StoreParametersForRoundTrip(OpenAuthenticationParameters parameters)
        {
            var session = GetSession();

            session["externalauth.parameters"] = parameters;
        }