2012-06-19 28 views
1

我试图在IIS 7.0中的httpmodulle中设置一个servervariable(“LOGON_USER”),但我没有实现它。在IIS 7.0中设置一个ServerVariable httpmodule

我的BeginRequest功能,那么远,

 BindingFlags temp = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; 


     MethodInfo addStatic = null; 
     MethodInfo makeReadOnly = null; 
     MethodInfo makeReadWrite = null; 


     Type type = application.Request.ServerVariables.GetType(); 
     MethodInfo[] methods = type.GetMethods(temp); 
     foreach (MethodInfo method in methods) 
     { 
      switch (method.Name) 
      { 
       case "MakeReadWrite": makeReadWrite = method; 
        break; 
       case "MakeReadOnly": makeReadOnly = method; 
        break; 
       case "AddStatic": addStatic = method; 
        break; 
      } 
     } 

     makeReadWrite.Invoke(application.Request.ServerVariables, null); 
     string[] values = { "LOGON_USER", "test" }; 
     addStatic.Invoke(application.Request.ServerVariables, values); 
     makeReadOnly.Invoke(application.Request.ServerVariables, null); 

在我搜索我读过这个解决方案会工作在较旧的IIS的,而不是在IIS 7.0或7.5。

有关如何在IIS 7.0中完成任何想法?

感谢

回答

1

解决:

HttpApplication application = (HttpApplication)source; 
    HttpContext context = application.Context; 

    context.User = new GenericPrincipal(new GenericIdentity("test"), null); 

new GenericPrincipal(new GenericIdentity("test"), null)将会把 “测试” 在LOGON_USER变量

点击此处了解详情:http://learn.iis.net/page.aspx/170/developing-a-module-using-net/