重新初始化MembershipProvider ASP.NET

时间:2013-09-30 15:44:10

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

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

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

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

1 个答案:

答案 0 :(得分:0)

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

    //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