实现自定义MembershipUser

时间:2009-05-26 18:49:25

标签: asp.net membership

我将绕圈子并且需要一些帮助来实现自定义MembershipUser,以便我可以将自己的自定义属性添加到MembershipUser。

我一直关注此网站上的示例:How to: Implement a Custom Membership User

我认为我遇到的问题是在CustomMembershipUser的构造函数中。

我的CustomMembershipUser有以下三个附加属性:firstName,middleName,lastName。

public class CustomMembershipProvider : MembershipProvider
{
  public override MembershipUser GetUser(string username, bool userIsOnline)
    {
       //.... Get data from database
       MembershipUser baseUser = new MembershipUser(this.Name,
                                               username,
                                               userId,
                                               email,
                                               "",
                                               comment,
                                               isApproved,
                                               isLockedOut,
                                               dtCreate,
                                               dtLastLogin,
                                               dtLastActivity,
                                               DateTime.Now,
                                               dtLastLockoutDate);
                    return new CustomMembershipUser(baseUser, firstName, middleName, lastName)
    }
}


public class CustomMembershipUser : MembershipUser
{
  private string _firstName;
    public string FirstName { get { return _firstName; } set { _firstName = value; } }

    private string _middleName;
    public string MiddleName { get { return _middleName; } set { _middleName = value; } }

    private string _lastName;
    public string LastName { get { return _lastName; } set { _lastName = value; } }

 public CustomMembershipUser(MembershipUser baseuser, string firstname, string middlename, string lastname)
    {
        _firstName = firstname;
        _middleName = middlename;
        _lastName = lastname;

        new CustomMembershipUser(baseuser); // DO I NEED THIS?? HOW TO IMPLEMENT??

    }
}

我这样称呼它:

    MembershipUser mu = Membership.GetUser(UserName);

    CustomMembershipProvider p = (CustomMembershipProvider)Membership.Provider;

    MembershipUser memUser = p.GetUser(UserName, true);

    object userId = memUser.ProviderUserKey;

ProviderUserKey为null,其他值也是如此。

如何获得我添加的添加属性?

由于

3 个答案:

答案 0 :(得分:12)

根据我自己尝试做同样事情的经验,尝试使用MembershipProvider来做这件事最终会令人沮丧和违反直觉。

成员资格提供者模型的想法不是改变或扩充用户的定义,正如您尝试的那样 - 它是允许框架另一种方法来访问已经存在的信息定义为属于“MembershipUser”。

我认为您真正想要的是用户个人资料。使用ASP.NET配置文件比实现自己的提供程序更容易。 You can find the overview here

答案 1 :(得分:11)

这对我有用:

public class CustomMembershipUser : MembershipUser
{
    public CustomMembershipUser(
        string providerName,
        string name,
        object providerUserKey,
        string email,
        string passwordQuestion,
        string comment,
        bool isApproved,
        bool isLockedOut,
        DateTime creationDate,
        DateTime lastLoginDate,
        DateTime lastActivityDate,
        DateTime lastPasswordChangedDate,
        DateTime lastLockoutDate
        )
        : base(providerName, name, providerUserKey, email, passwordQuestion,
        comment, isApproved, isLockedOut, creationDate, lastLoginDate,
        lastActivityDate, lastPasswordChangedDate, lastLockoutDate)
    {
    }

    // Add additional properties
    public string CustomerNumber { get; set; }

}

public class CustomMembershipProvider : MembershipProvider
{

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        if (string.IsNullOrEmpty(username))
        {
            // No user signed in
            return null;
        }

        // ...get data from db

        CustomMembershipUser user = new CustomMembershipUser(
                    "CustomMembershipProvider",
                    db.Username,
                    db.UserId,
                    db.Email,
                    "",
                    "",
                    true,
                    false,
                    db.CreatedAt,
                    DateTime.MinValue,
                    DateTime.MinValue,
                    DateTime.MinValue,
                    DateTime.MinValue);

        // Fill additional properties
        user.CustomerNumber = db.CustomerNumber;

        return user;

    }

}

// Get custom user (if allready logged in)
CustomMembershipUser user = Membership.GetUser(true) as CustomMembershipUser;

// Access custom property
user.CustomerNumber

答案 2 :(得分:3)

就在你知道的时候,我曾试图沿着MembershipProvider路径走下去,这是一个漫长而多风的路径。您可能会看到,只创建实现IPrincipal和IIdentity的类将满足您的需求,因为它们需要的开销更少。