实施自定义会员用户

发布于 2024-07-21 14:37:00 字数 2492 浏览 8 评论 0原文

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

我一直在关注此网站上的示例:如何:实现自定义会员用户< /a>

我认为,我遇到的问题出在 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 为空,其他值也是如此。

如何获取我添加的附加属性?

谢谢

I am going round in circles and need some help in implementing a Custom MembershipUser so that I can add my own custom Properties to the MembershipUser.

I have been following the example on this site: How to: Implement a Custom Membership User

The problem I am having is in the constructor of CustomMembershipUser, I think.

My CustomMembershipUser has these three additional Properties: 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??
        
    }
}

I am calling it like so:

    MembershipUser mu = Membership.GetUser(UserName);

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

    object userId = memUser.ProviderUserKey;

The ProviderUserKey is null and so are the other values.

How can I obtain the addition Properties I added?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

本王不退位尔等都是臣 2024-07-28 14:37:00

这对我有用:

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

This is working for me:

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
差↓一点笑了 2024-07-28 14:37:00

根据我自己尝试做大部分相同事情的经验,尝试使用 MembershipProvider 来执行此操作最终将是令人沮丧且违反直觉的体验。

成员资格提供者模型的想法并不是像您尝试做的那样更改或增强用户的定义 - 它是允许框架以替代方式访问已定义为所属的信息至“会员用户”。

我认为您真正需要的是用户个人资料。 使用 ASP.NET 配置文件比实现您自己的提供程序要容易得多。 您可以在此处找到概述

Based on my own experience trying to do much of the same, trying to use the MembershipProvider to do this will be an ultimately frustrating and counterintuitive experience.

The idea of the membership provider model isn't to change or augment what the definition of a user is, as you're trying to do - it is to allow the Framework an alternate means of accessing the information that has already been defined as belonging to a "MembershipUser".

I think what you're really looking for is a user profile. Using ASP.NET profiles is boatloads easier than implementing your own provider. You can find the overview here.

記憶穿過時間隧道 2024-07-28 14:37:00

正如您所知,我之前曾尝试沿着 MembershipProvider 的路径走下去,这是一条漫长而艰难的道路。 您可能会发现,仅创建实现 IPrincipal 和 IIdentity 的类是否就能满足您的需求,因为它们需要的开销要少得多。

Just so you know, I've tried to go down the MembershipProvider path before, and it's a long and windy one. You might see if just creating classes that implement IPrincipal and IIdentity will satisfy your needs, since they entail a lot less overhead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文