Implementing Custom Membership user and Custom Membership Provider

梦想的初衷 提交于 2019-12-30 07:10:57

问题


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?

回答1:


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.




回答2:


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();



来源:https://stackoverflow.com/questions/8090350/implementing-custom-membership-user-and-custom-membership-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!