检查 Active Directory 角色花费的时间太长
我有这段代码来检查组成员资格,但似乎需要很长时间才能响应并减慢我的应用程序的速度,几乎需要 7-12 秒才能响应,我只需要检查一个特定的组成员资格,是否有更快的方法方法来做到这一点?
public static bool isInRole(UserAccount userAccount, string groupName)
{
using (var ctx = new PrincipalContext(ContextType.Domain, userAccount.DomainName))
{
using (var grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName))
{
bool isInRole = grp != null &&
grp
.GetMembers(true)
.Any(m => m.SamAccountName == userAccount.UserName);
return isInRole;
}
}
i have this code to check for group membership but it seems to be taking too long to respond and slowing down my app, it takes almost 7-12 sec to respond, i just need to check for one particular group membership, is there a faster way to do this?
public static bool isInRole(UserAccount userAccount, string groupName)
{
using (var ctx = new PrincipalContext(ContextType.Domain, userAccount.DomainName))
{
using (var grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName))
{
bool isInRole = grp != null &&
grp
.GetMembers(true)
.Any(m => m.SamAccountName == userAccount.UserName);
return isInRole;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我手头没有您的特定 AD 来测试这一点 - 但可能值得一试:与其检查特定用户的组成员(可能有数千名成员),不如检查用户的组成员身份以查看用户是否拥有正确的组?
比如:
也许这样事情会更快一点?
I don't have your specific AD at hand to test this - but it might be worth an attempt: instead of checking the group's members for a particular users (there could potentially be thousands of members), why don't you check the user's group membership to see if the user has the right group??
Something like :
Maybe that way around things will be a bit faster?
我无法给您答案,因为我们没有足够的信息,只能追踪您的问题...
首先,尝试使用标准工具(在网络服务器上运行)查询 AD - 这同样慢吗?如果是这样,则可能是网络/DC 问题。
假设只是您的实现速度慢...
您是否正在调用 需要时间来编译/加载?要测试这一点,请执行多个调用并比较延迟 - 如果第一个调用明显较长,请查看您的 IIS 设置 0 您可以增加应用程序池卸载之前的空闲时间等来缓解问题。
如果您确定代码花费的时间太长,请使用 Visual Studio Pofiler 它将识别哪些函数/调用导致延迟 - 如果它在您的代码中,请优化它,如果它在框架中,那么您要么使用错误,要么您发现了问题框架(不太可能)。
如果您可以编辑您的问题以包含上述问题的答案,我们也许可以提供进一步的帮助
编辑:针对有关 WCF 中缓存的问题,有多种方法可以解决此问题 - 您可以替换服务类的持久性提供程序以使其成为单例 - 然后您可以仅使用私有变量/内存进行缓存。这要求您的类是线程安全的。
替代方案包括:数据库、文件系统、应用程序范围内的内存缓存 (System.运行时.缓存)
I can't give you an answer as we don't have enough information but to track down your problem...
Firstly, try querying AD using the standard tools (run on the web server) - is this just as slow? If so, it's likely a network/DC issue.
Assuming it's just your implementation that's slow...
Are you calling a WCF service hosted in a website that's taking time to compile/load? To test this, do multiple calls and compare delays - If the first is significantly longer, have a look at your IIS settings 0 You can increase idle time before app pool unload, etc. to mitigate the problem.
If You're certain it's that your code taking too long, use the Visual Studio Pofiler which will identify which functions/calls are causing the delay - If it's in your code, optimise it, If it's in the framework then you're either using it wrong or you've found a problem with the framework (unlikely).
If you can edit your question to include answers to the above, we may be able to help further
Edit: In response to the question about caching within WCF there are a number of ways to approach this - You can replace the Persistence provider for the service class to make it a singleton - then you can just use private variables/memory for caching. This requires your class to be thread-safe.
Alternatives include: database, file system, app-wide in-memory cache (System.Runtime.Caching)