开发者

Custom MembershipProvider without 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 prop开发者_运维问答erties I really don't want or need.


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜