title: Orchard Custom MembershipService and UserService
body: <p>I am creating a new module to authenticate users against a custom MembershipService.  I have made copies of the MembershipService and UserService from the Orchard.Users module and added the OrchardSuppressDependency attribute to each.  I also created a custom data access service and injected this into each.  My first step is to replace the ValidateUser method.</p>
  
  <p>The existing method uses a ContentManager query to retrieve the UserPart from the Orchard database.</p>
  
  <pre><code>public IUser ValidateUser(string userNameOrEmail, string password)
      {
          var lowerName = userNameOrEmail == null ? "" : userNameOrEmail.ToLowerInvariant();
  
          var user = _orchardServices.ContentManager.Query&lt;UserPart, UserPartRecord&gt;().Where(u =&gt; u.NormalizedUserName == lowerName).List().FirstOrDefault();
  
          if (user == null)
              user = _orchardServices.ContentManager.Query&lt;UserPart, UserPartRecord&gt;().Where(u =&gt; u.Email == lowerName).List().FirstOrDefault();
  
          if (user == null || ValidatePassword(user.As&lt;UserPart&gt;().Record, password) == false)
              return null;
  
          if (user.EmailStatus != UserStatus.Approved)
              return null;
  
          if (user.RegistrationStatus != UserStatus.Approved)
              return null;
  
          return user;
      }
  </code></pre>
  
  <p>I need to create a UserPart with data from my own repository.  I attempted the following and no exceptions are thrown, but the user is not logged in.</p>
  
  <pre><code>public IUser ValidateUser(string userNameOrEmail, string password)
      {
          UserPart user = null;
          var userRecord = _userRepository.GetUserByEmail(userNameOrEmail);
  
          if (null != userRecord)
          {
              user = _orchardServices.ContentManager.New&lt;UserPart&gt;("User");
              user.Record.Email = userRecord.Email;
              user.Record.EmailChallengeToken = userRecord.EmailChallengeToken;
              user.Record.EmailStatus = userRecord.EmailStatus;
              user.Record.HashAlgorithm = userRecord.HashAlgorithm;
              user.Record.Id = userRecord.Id;
              user.Record.NormalizedUserName = userRecord.NormalizedUserName;
              user.Record.Password = userRecord.Password;
              user.Record.PasswordFormat = userRecord.PasswordFormat;
              user.Record.PasswordSalt = userRecord.PasswordSalt;
              user.Record.RegistrationStatus = userRecord.RegistrationStatus;
              user.Record.UserName = userRecord.UserName;
          }
  
          if (user == null || ValidatePassword(user.Record, password) == false)
              return null;
  
          if (user.EmailStatus != UserStatus.Approved)
              return null;
  
          if (user.RegistrationStatus != UserStatus.Approved)
              return null;
  
          return user;
      }
  </code></pre>
  
  <p>Is there a way to populate a UserPart without a ContentManager query?  I also posted on the Orchard discussion forum <a href="http://orchard.codeplex.com/discussions/264466" rel="nofollow">here</a>.</p>