3

References

http://msdn.microsoft.com/en-us/library/6tc47t75%28v=VS.80%29.aspx

http://msdn.microsoft.com/en-us/library/ms366730.aspx

Question

In the 2nd link precisely under heading Create a Custom Membership Provider you will note that they mention this

You will need to create a custom membership provider that supports both your custom membership user type, and your custom membership data store. The GetUser and CreateUser methods of the custom membership provider can be written to return objects of the custom membership user type.

below is my custom membership user with custom fields

Custom Membership User

 public class CustomMembershipUser : MembershipUser
    {
        //private fields for internal use
        private bool _isMarried;
        private bool _hasLicense;
        private string _address;
        private int _userId;

        //public fields for use by end user

        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }
...

Custom Membership Provider

public class CustomMembershipProvider : MembershipProvider
    {
        .....

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }
....

I did exactly what they told me to do, return membership user of my custom type like this

public override **CustomMembershipUser** CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)

but then i was complained by the IDE that Cannot Change return type when overriding MembershipProvider.CreateUser. So what am i supposed to do now,

  • Create another method with same parameters and return custom membership user?
  • I don't have any need for password question, security question fields how do i get rid of them?
Deeptechtons
  • 10,945
  • 27
  • 96
  • 178

2 Answers2

4

You need to return a MembershipUser instance in the function which is the return type for it. Currently it isn't returning what it is supposed to return.

There is a good tutorial at codeproject : Custom Membership Providers. You might find it helpful in understanding where you aren't getting right.

Pankaj Upadhyay
  • 12,966
  • 24
  • 73
  • 104
  • you failed to answer the question 2 from the bullets at the end of post – Deeptechtons Nov 11 '11 at 06:38
  • You will have to implement them. It comes as a package :-) . The MembershipProvider abstract class inherits the ProviderBase abstract class from the System.Configuration.Provider namespace, so you must implement the required members of the ProviderBase class as well. Visit [Required MembershipProvider Members](http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx) for knowing what needs to be implemented in order to get it working – Pankaj Upadhyay Nov 11 '11 at 06:49
  • just another thought, what if i inherited providerbase and implement my own version of MembershipProvider will it work with the membership components. – Deeptechtons Nov 11 '11 at 06:53
  • 1
    hmm...in that case you can get rid of them. But then again why reinvent the wheel. You will need to do a lot(a lot) more work to implement ProviderBase directly for just a little requirement – Pankaj Upadhyay Nov 11 '11 at 07:05
  • one more question, if membership user provides all info about user what is the use of a `Profile Provider` explain as you would for a layman. – Deeptechtons Nov 11 '11 at 07:22
  • 1
    You are getting bit confused. This is what these particular providers offer you with : (1) **Membership** - Authentication for a user (username/password) (2) **Role** - Authorization for a user (IsAdmin? IsPowerUser?) (3) **Profile**- Details about a user (phone number, age, date of birth) – Pankaj Upadhyay Nov 11 '11 at 07:30
-1

You should not create a new method that return CustomMembershipUser.

You should have only MembershipUser CreateUser(...) in the custom membership provider. Before your return you need to cast the CustomMembershipUser to be MembershipUser.

E.g. out following when you return.

return (MembershipUser)new CustomMembershipUser();

Pongsathon.keng
  • 1,417
  • 11
  • 14