2

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

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

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

4

1 回答 1

0

I have finally figured out what to do in this case:

    //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);
    }

    ....

Then for any method I dont want to implement (for instance, I don't want to change any data on the AD server, just want to list and verify users) I just have a NotImplementedException

于 2013-11-21T10:05:02.177 回答