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.
The existing method uses a ContentManager query to retrieve the UserPart from the Orchard database.
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;
}
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.
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.UserNam开发者_JS百科e;
}
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;
}
Is there a way to populate a UserPart without a ContentManager query? I also posted on the Orchard discussion forum here.
I would recommend you read the code for DefaultContentManager very closely. I don't quite understand what you are trying to do, but you don't need to do "As" on user, as it already is a UserPart.
Make sure GetAuthenticatedUser() method present in file FormsAuthenticationService.cs returns your user as expected.
精彩评论