2012-06-19 60 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/