【问题标题】:Reinitialise MembershipProvider ASP.NET重新初始化 MembershipProvider ASP.NET
【发布时间】:2013-09-30 15:44:10
【问题描述】:

我有一个 ActiveDirectoryMembershipProvider 和一个 SQLMembershipProvider(本质上,它是一个 ASP.Net 应用程序,为应用程序生成的用户和来自活动目录的用户都可以登录)。我想设置登录控制器,以便当本地用户登录时 AD-Provider 无法与 AD 服务器通信,当另一个用户尝试登录时,它将重新初始化 ADProvider。

据我所知,应用程序会在调用任何一个提供程序时初始化两个提供程序。 ADProvider 尝试连接,如果无法连接,则会引发错误。 我目前发现错误并默默丢弃。本地用户可以登录,但是当我重新启动我的 AD 服务器时,我似乎无法找到让 ADProvider 尝试重新连接的方法。我曾尝试再次调用 initialize(),但它会引发“已初始化”错误。

如果我需要创建自己的,很好,我希望我缺少一些配置或简单的方法。

【问题讨论】:

    标签: c# asp.net active-directory membership-provider activedirectorymembership


    【解决方案1】:

    我终于想出了在这种情况下该怎么做:

        //create our own AD Provider
        private System.Web.Security.ActiveDirectoryMembershipProvider base_provider;
    
        private bool initialised = false;
    
        //these values are saved when Init if first called by the Membership Collection. 
        //So that I can pass them back into the new base_provider when it is made.
        private string name = null;
        private System.Collections.Specialized.NameValueCollection config;
        private Exception init_exception;
    
        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            this.name = name;
            this.config = config;
    
            try
            {
                base_provider = new System.Web.Security.ActiveDirectoryMembershipProvider();
    
                base_provider.Initialize(name, config);
                initialised = true;
            }
            catch (Exception e)
            {
                this.base_provider = null;
                init_exception = e;
            }
        }
    
        public override System.Web.Security.MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            if (!initialised) this.Initialize(this.name, this.config);
    
            //just a custom exception that my Membership object (that handles calling the two providers) picks up on
            if (base_provider == null) throw new NotInitialisedException(this.init_exception.Message);
    
            return base_provider.GetAllUsers(pageIndex, pageSize, out totalRecords);
        }
    
        ....
    

    然后对于我不想实现的任何方法(例如,我不想更改 AD 服务器上的任何数据,只想列出和验证用户)我只有一个 NotImplementedException

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      相关资源
      最近更新 更多