public HttpContext FakeHttpContext(Dictionary <string, object> sessionVariables, string path) { var httpRequest = new HttpRequest(string.Empty, path, string.Empty); var stringWriter = new StringWriter(); var httpResponce = new HttpResponse(stringWriter); var httpContext = new HttpContext(httpRequest, httpResponce); httpContext.User = new GenericPrincipal(new GenericIdentity("username"), new string[0]); Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("username"), new string[0]); var sessionContainer = new HttpSessionStateContainer( "id", new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 10, true, HttpCookieMode.AutoDetect, SessionStateMode.InProc, false); foreach (var var in sessionVariables) { sessionContainer.Add(var.Key, var.Value); } SessionStateUtility.AddHttpSessionStateToContext(httpContext, sessionContainer); return(httpContext); }
public static HttpContext FakeHttpContext(Dictionary <string, object> sessionVariables, string path) { string username = "******"; string userid = "15e50543-7897-48d2-a8c8-f1ca967e3ca3"; //could be a constant List <Claim> claims = new List <Claim> { new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", username), new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", userid) }; var genericIdentity = new GenericIdentity(""); genericIdentity.AddClaims(claims); var genericPrincipal = new GenericPrincipal(genericIdentity, new string[] { "*****@*****.**" }); var httpRequest = new HttpRequest(string.Empty, path, string.Empty); var stringWriter = new StringWriter(); var httpResponce = new HttpResponse(stringWriter); var httpContext = new HttpContext(httpRequest, httpResponce); httpContext.User = genericPrincipal; Thread.CurrentPrincipal = genericPrincipal; var sessionContainer = new HttpSessionStateContainer( "id", new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 10, true, HttpCookieMode.AutoDetect, SessionStateMode.InProc, false); foreach (var var in sessionVariables) { sessionContainer.Add(var.Key, var.Value); } SessionStateUtility.AddHttpSessionStateToContext(httpContext, sessionContainer); return(httpContext); }