using System.Web; using System.Web.SessionState; using System.Security.Principal; using System.IO;
// ...
HttpContext.Current = new HttpContext(new HttpRequest("", "http://localhost", ""), new HttpResponse(new StringWriter()));
// 允許使用Session SessionStateUtility.AddHttpSessionStateToContext(HttpContext.Current, new HttpSessionStateContainer("", new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 20000, true, HttpCookieMode.UseCookies, SessionStateMode.Off, false));
// 模擬user登入 HttpContext.Current.User = new GenericPrincipal(new GenericIdentity("user"), null);
// 模擬未登入使用者 //HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(""), null);