没有 MembershipUser 的自定义 MembershipProvider

发布于 2024-12-02 07:33:21 字数 133 浏览 1 评论 0原文

是否可以在不使用 MembershipUser 类的情况下创建自定义 MembershipProvider ?

我想使用我自己的 User 类并保持整洁,我不想使用 MembershipUser 类,它有很多我真的不想要或不需要的属性。

Is it possible to create a custom MembershipProvider without the use of the MembershipUser class?

I'd like to use my own User class and to keep things tidy, I'd like to not have to use the MembershipUser class which has a lot of properties I really don't want or need.

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

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

发布评论

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

评论(4

风渺 2024-12-09 07:33:21

不,不是。但您始终可以使用 Repository 模式或类似模式在其之上编写自己的抽象,然后在应用程序中使用您自己的 User 模型。

public UserRepository : IUserRepository
{
   private MembershipProvider provider;
   private UserAdapter userAdapter;

   public UserService(MembershipProvider provider, UserAdapter userAdapter)
   {
       this.provider = provider;
       this.userAdapter = userAdapter;
   }

   public MyUser GetUser(string email)
   {
       MembershipUser user = provider.GetUser(username, false);
       MyUser myUser= userAdapter.Map(user);
       return myUser;
   }
}

No it's not. But you can always write your own abstraction on top of it using a Repository pattern or similar, and then use your own User model in the application.

public UserRepository : IUserRepository
{
   private MembershipProvider provider;
   private UserAdapter userAdapter;

   public UserService(MembershipProvider provider, UserAdapter userAdapter)
   {
       this.provider = provider;
       this.userAdapter = userAdapter;
   }

   public MyUser GetUser(string email)
   {
       MembershipUser user = provider.GetUser(username, false);
       MyUser myUser= userAdapter.Map(user);
       return myUser;
   }
}
初见你 2024-12-09 07:33:21

如果您确实想在 ASP.NET 会员基础设施中内置您的提供程序,那么您应该这样做。

Membership.Provider 返回抽象 MembershipProvider 还包含:

public abstract MembershipUser CreateUser(..);
public abstract MembershipUser GetUser(..);

以及返回 MembershipUser 类。

If you really want to build-in your provider in ASP.NET Membership infrastructure, you should.

Membership.Provider returns abstract MembershipProvider also containing:

public abstract MembershipUser CreateUser(..);
public abstract MembershipUser GetUser(..);

and other methods returning MembershipUser class.

很快妥协 2024-12-09 07:33:21

您可以创建自定义会员资格提供程序,而无需根据您的使用情况使用/继承会员用户。如果您没有使用内置的 createuser 方法(用户是在另一个源系统中创建的),那么您确实不需要成员资格用户对象。否则,只需创建另一个使用您的额外属性从membershipuser 继承的对象。

You can create a custom memembership provider without using/inheriting from the membership user depending upon your usage. If you are not using the built in createuser method (the user is created in another source system), then you really don't need the membership user object. Otherwise, just create another object that inherits from membershipuser with your extra properties.

伴我老 2024-12-09 07:33:21

这是我的解决方案

customerUser.cs

public class DZMembership
{
    public virtual Guid Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual string Password { get; set; }
    public virtual DateTime TimeCreated { get; set; }

}

customerProvider.cs

 public class DZMembershipProvider : MembershipProvider
{

    IDALMembership dal = DalFactory.GetDalMembership();
    #region override of membership provider
    public override string ApplicationName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public override bool ChangePassword(string username, string oldPassword, string newPassword)
    {
        DZMembership member = dal.GetMemberByName(username);
        string encryptedOldPsw =  FormsAuthentication.HashPasswordForStoringInConfigFile(oldPassword, "MD5");
        string encryptedNewPsw = FormsAuthentication.HashPasswordForStoringInConfigFile(newPassword, "MD5");
        if (member.Password != encryptedOldPsw) return false;
        member.Password = encryptedNewPsw;
        dal.ChangePassword(member);
        return true;
    }
public override MembershipUser GetUser(string username, bool userIsOnline)
    {

        DZMembership user = dal.GetMemberByName(username);
        if (user == null) return null;
        MembershipUser mu = new MembershipUser("DZMembershipProvider",
             username, user.Id, "", "", string.Empty,
             true, true, DateTime.Now,
             DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
        return mu;

    }

当该方法需要返回 MembershipUser 类型时,请使用客户用户的信息构建一个新类型。大多数情况下,都会好的。因为内置控件(登录,登录状态。)不需要更多。但我建议通过继承 MembershipUser 来自定义您自己的用户,附加属性不会让您烦恼,但缺乏会。

this is my solution

customerUser.cs

public class DZMembership
{
    public virtual Guid Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual string Password { get; set; }
    public virtual DateTime TimeCreated { get; set; }

}

customerProvider.cs

 public class DZMembershipProvider : MembershipProvider
{

    IDALMembership dal = DalFactory.GetDalMembership();
    #region override of membership provider
    public override string ApplicationName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public override bool ChangePassword(string username, string oldPassword, string newPassword)
    {
        DZMembership member = dal.GetMemberByName(username);
        string encryptedOldPsw =  FormsAuthentication.HashPasswordForStoringInConfigFile(oldPassword, "MD5");
        string encryptedNewPsw = FormsAuthentication.HashPasswordForStoringInConfigFile(newPassword, "MD5");
        if (member.Password != encryptedOldPsw) return false;
        member.Password = encryptedNewPsw;
        dal.ChangePassword(member);
        return true;
    }
public override MembershipUser GetUser(string username, bool userIsOnline)
    {

        DZMembership user = dal.GetMemberByName(username);
        if (user == null) return null;
        MembershipUser mu = new MembershipUser("DZMembershipProvider",
             username, user.Id, "", "", string.Empty,
             true, true, DateTime.Now,
             DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
        return mu;

    }

when the method need returning a MembershipUser type, build a new one with your customer user's infomation . in most case, it will be fine. because the build-in Controls(login, loginstatus.) doesn't need more than those. but i sugguest custom your ownuser by inheriting the MembershipUser,the additional properties will not bite you ,but the lackness will.

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