我有一个 ActiveDirectoryMembershipProvider 和一个 SQLMembershipProvider(本质上,它是一个 ASP.Net 应用程序,其中为应用程序生成的用户和事件目录中的用户都可以登录)。我想设置登录 Controller ,这样如果本地用户登录时 AD-Provider 无法与 AD 服务器通信,它将在另一个用户尝试登录时重新初始化 ADProvider。
据我所知,应用程序会在调用任一提供程序时初始化这两个提供程序。 ADProvider 尝试连接,如果无法连接,则会抛出错误。 我目前捕获错误并默默丢弃。本地用户可以登录,但是当我重新启动我的 AD 服务器时,我似乎无法找到让 ADProvider 尝试重新连接的方法。我尝试再次调用 initialize(),但它抛出“已初始化”错误。
如果我需要创建自己的,很好,我希望有一些我缺少的配置或简单方法。
最佳答案
我终于想出了在这种情况下该怎么做:
//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
关于c# - 重新初始化 MembershipProvider ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19098396/