在IIS 7.0 httpmodule中设置ServerVariable

时间:2012-06-19 15:41:39

标签: iis iis-7 iis-7.5 httpmodule server-variables

我正在尝试在IIS 7.0中的httpmodulle中设置servervariable(“LOGON_USER”),但我并没有达到目的。

我的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中完成任何想法?

由于

2 个答案:

答案 0 :(得分:1)

解决:

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

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

new GenericPrincipal(new GenericIdentity("test"), null)会将“test”放在 LOGON_USER 变量中

此处提供更多信息:http://learn.iis.net/page.aspx/170/developing-a-module-using-net/

答案 1 :(得分:0)

使用IIS的URL重写模块设置服务器变量时,必须明确指定允许设置的服务器变量。这对您自己的代码也许是必要的。请参阅:http://learn.iis.net/page.aspx/686/setting-http-request-headers-and-iis-server-variables/(并搜索“允许更改服务器变量”)