gpt4 book ai didi

asp.net - WindowsTokenRoleProvider的性能不佳

转载 作者:行者123 更新时间:2023-12-04 03:41:32 25 4
gpt4 key购买 nike

我正在使用WindowsTokenRoleProvide r来确定ASP.NET Web应用程序中的Active Directory组成员身份。

我的问题是性能不好,尤其是当用户处于多个组中时。例如,我属于253(!)个小组,而WindowsTokenRoleProvider大约需要150秒才能确定我属于哪个小组。

我知道我可以使用缓存,这样就不会在随后对用户的请求上执行此操作,但是显然在第一次点击时花这么长时间是 Not Acceptable 。

我有什么选择?我可以强制WindowsTokenRoleProvider仅考虑某些组吗? (我只对5感兴趣)。

最佳答案

一些测试表明,我的问题是调用:

Roles.IsUserInRole(groupName)

正在访问 GetRolesForUser中的 RoleProvider方法-正在检索用户所属的每个角色的详细信息。

但是调用:
Roles.Provider.IsUserInRole(groupName)

确定用户是否在组中-无需检索用户所在的每个角色的详细信息。

很奇怪,但是看起来使用 Roles.Provider.IsUserInRole可以解决我的问题。

*更新*

事实证明,这只是部分解决方法;如果我使用命令性权限检查,或者在web.comfig中使用“allow”和“deny”,那么 WindowsTokenRoleProvider仍然会继续缓慢地获取用户所属的每个组的详细信息:o(

所以我的问题仍然存在...

*更新*

我通过创建一个从WindowsTokenRoleProvider扩展的类并覆盖 GetRolesForUser来解决此问题,因此它仅检查配置中指定的角色的成员身份。它也包括缓存:
/// <summary>
/// Retrieve the list of roles (Windows Groups) that a user is a member of
/// </summary>
/// <remarks>
/// Note that we are checking only against each system role because calling:
/// base.GetRolesForUser(username);
/// Is _very_ slow if the user is in a lot of AD groups
/// </remarks>
/// <param name="username">The user to check membership for</param>
/// <returns>String array containing the names of the roles the user is a member of</returns>
public override string[] GetRolesForUser(string username)
{
// Will contain the list of roles that the user is a member of
List<string> roles = null;

// Create unique cache key for the user
string key = String.Concat(username, ":", base.ApplicationName);

// Get cache for current session
Cache cache = HttpContext.Current.Cache;

// Obtain cached roles for the user
if (cache[key] != null)
{
roles = new List<string>(cache[key] as string[]);
}

// Was the list of roles for the user in the cache?
if (roles == null)
{
roles = new List<string>();

// For each system role, determine if the user is a member of that role
foreach (SystemRoleElement role in WebConfigSection.Settings.SystemRoles)
{
if (base.IsUserInRole(username, role.Name))
{
roles.Add(role.Name);
}
}

// Cache the roles for 1 hour
cache.Insert(key, roles.ToArray(), null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
}

// Return list of roles for the user
return roles.ToArray();
}

关于asp.net - WindowsTokenRoleProvider的性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1929073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com