2011-07-05 54 views
2

我想每天缓存活动目录数据,例如电子邮件,名字和姓氏两次。这应该在global.aspx application.start()中出现在幕后。现在,我想要获取1700(我的应用程序允许浏览用户的功能)用户,我会从缓存的数据中做到这一点。我的代码在Asp.net C#中。缓存Active Directory数据

有人可以让我知道如何做到这一点。

回答

5

我的代码在第一次访问时运行(您可以通过在global.asax文件中放置几行来开始此应用程序)。它将缓存的数据存储在ASP.Net缓存中12小时。 12小时后,缓存将在下次访问数据时刷新。

public class UserInfo 
{ 
    public static List<UserInfo> UserInfoCache { 
     get{ 
      if(HttpContext.Current.Cache["CachedUserList"] == null) { 
       PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

       PrincipalSearcher ps = new PrincipalSearcher(new UserPrincipal(ctx) { SamAccountName = "*" }); 

       var users = ps.FindAll(); 

       var result = from c in users 
          let u = (UserPrincipal)c 
          select new UserInfo() { Email = u.EmailAddress, FirstName = u.GivenName, LastName = u.Surname }; 

       HttpContext.Current.Cache.Insert("CachedUserList", result, null, DateTime.Now.AddHours(12), System.Web.Caching.Cache.NoSlidingExpiration); 
      } 

      return (List<UserInfo>)HttpContext.Current.Cache["CachedUserList"]; 
     } 
    } 

    public string Email { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 
+0

我做到了,但在Cache.Insert它显示我错误:值不能为空。参数名称:值 –

+0

它工作正常.. :) :)传递一个空值..thnak你 –