Translate

Tuesday, July 31, 2012

MS Test

CustomIdentity userIdentity = new CustomIdentity();
CustomPrincipal principal = new CutomPrincipal();

HttpContext.Current = new HttpContext(new HttpRequest("", "http://localhost/", ""),
  New HttpResponse(new System.IO.StringWriter()));
System.Web.SessionState.SessionStateUtility.AddHttpSessionStateToContext(
        HttpContext.Current, new System.Web.SessionState.HttpSessionStateContainer("",
        new System.Web.SessionState.SessionStateItemCollection(),
        new HttpStaticObjectsCollection(), 20000, true,
        HttpCookie.userCookies, Ssystem Web.SessionState.SessionStateMode.Off, false));
HttpContext.Current.User = principal;
//do test stuff here with web app

Public class CustomPrincipal : System.Security.Principal.IPrincipal
{
        Public System.Security.Principal.IIdentity Identity
        {
                get { return new System.Security.Principla.GenericIdentity("USER_NAME");}
        }

        public bool IsInRole(string role)
        {
                return true;
        }
}

Public class CustomIdentity : System.SecurityPrincipal.IIdentity
{
        Public string AuthenticationType
        {
                Get { return "Basic";}
        }

        Public bool IsAuthenticated
        {
                Get { return true; }
        }

        Public string Name
        {
                Get { return "USER_NAME"; }
        }
}

        [TestCleanup]
        Public void Clean()
        {
                HttpContext.Current = null;
        }