Custom MembershipService

Topics: Writing modules
Jul 8, 2011 at 9:01 PM
Edited Jul 12, 2011 at 6:40 PM

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 the replace the ValidateUser method.

public IUser ValidateUser(string userNameOrEmail, string password)
        {
            var lowerName = userNameOrEmail == null ? "" : userNameOrEmail.ToLowerInvariant();

            var user = _orchardServices.ContentManager.Query<UserPart, UserPartRecord>().Where(u => u.NormalizedUserName == lowerName).List().FirstOrDefault();

            if (user == null)
                user = _orchardServices.ContentManager.Query<UserPart, UserPartRecord>().Where(u => u.Email == lowerName).List().FirstOrDefault();

            if (user == null || ValidatePassword(user.As<UserPart>().Record, password) == false)
                return null;

            if (user.EmailStatus != UserStatus.Approved)
                return null;

            if (user.RegistrationStatus != UserStatus.Approved)
                return null;

            return user;
        }

 

How do I replace this call:

 

var user = _orchardServices.ContentManager.Query<UserPart, UserPartRecord>().Where(u => u.NormalizedUserName == lowerName).List().FirstOrDefault();

 

with something like this:

 

var user = _myNewUserService.GetUser(lowerName);

 

Is this the correct approach or is there an easier/better way to achieve this?

Jul 11, 2011 at 9:33 PM

I tried creating a new UserPart and setting the Record properties manually.  The code executes without error, but the user is not logged in.

public IUser ValidateUser(string userNameOrEmail, string password)
        {
            UserPart user = null;
            var userRecord = _userRepository.GetUserByEmail(userNameOrEmail);

            if (null != userRecord)
            {
                user = _orchardServices.ContentManager.New<UserPart>("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.As<UserPart>().Record, password) == false)
                return null;

            if (user.EmailStatus != UserStatus.Approved)
                return null;

            if (user.RegistrationStatus != UserStatus.Approved)
                return null;

            return user;
        }