How do I call Initialize on a custom MembershipProvider?

前端 未结 8 695
死守一世寂寞
死守一世寂寞 2020-12-08 08:17

I have read through all the related questions, but I still unable to get the right solution for some reason, something is not right on my side, but not sure what\'s causing

相关标签:
8条回答
  • 2020-12-08 09:16

    Here is the code to initialize a provider:

                    System.Collections.Specialized.NameValueCollection adProviderConfig;
                    adProviderConfig = membershipSection.Providers[adProviderName].Parameters;
                    var _ADProvider = new ActiveDirectoryMembershipProvider();
                    _ADProvider.Initialize(adProviderName, adProviderConfig);
    
    0 讨论(0)
  • 2020-12-08 09:19

    I am trying to work out what you have done.... I think you have proceeded as follows:

    • Created a custom class called MyMembershipProvider by inheriting from MembershipProvider
    • Configured web.config (looks correct to me)
    • Created a web control that fires an event to do something (e.g. to authenticate a login) Then...
    • Within this event, you have tried to do something like this and wondering why Initialize() isn't called while stepping through your code:

      MyNameSpace.MyMemberShipProvider msp = new MyNameSpace.MyMemberShipProvider();
      bool IsAuthorised = msp.ValidateUser(txtLogin, txtPass);

    Solution: - Use this instead:

    bool IsAuthorised = Membership.ValidateUser(txtLogin, txtPass);
    
    • Don't create an instance of your class by yourself, instead let .NET do this for you by using the Membership static class, which ensures that only one instance of MyMemberShipProvider exists throughout the lifetime of the application. If you instantiate your own class and call Initialize(), your code will not be thread safe.
    0 讨论(0)
提交回复
热议问题