Orchard Custom MembershipService和UserService

时间:2011-07-12 17:21:55

标签: c# asp.net-mvc orchardcms

我正在创建一个新模块,以根据自定义MembershipService对用户进行身份验证。我从Orchard.Users模块制作了MembershipService和UserService的副本,并为每个模块添加了OrchardSuppressDependency属性。我还创建了一个自定义数据访问服务并将其注入到每个服务中。我的第一步是替换ValidateUser方法。

现有方法使用ContentManager查询从Orchard数据库中检索UserPart。

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;
    }

我需要使用我自己的存储库中的数据创建一个UserPart。我尝试了以下操作,并且没有抛出任何异常,但用户未登录。

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.Record, password) == false)
            return null;

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

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

        return user;
    }

有没有办法在没有ContentManager查询的情况下填充UserPart?我还在Orchard论坛上发帖here

2 个答案:

答案 0 :(得分:0)

我建议你仔细阅读DefaultContentManager的代码。我不太明白你要做什么,但你不需要在用户上做“As”,因为它已经是UserPart。

答案 1 :(得分:0)

确保文件FormsAuthenticationService.cs中存在的GetAuthenticatedUser()方法按预期返回用户。