user1466890
user1466890

Reputation: 21

Set a ServerVariable in a IIS 7.0 httpmodule

I'm trying to set a servervariable ("LOGON_USER") in a httpmodulle in IIS 7.0, but I'm not archieving it.

My BeginRequest function, so far...

        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);

During my searches I've read that this solution would work on older IIS's, but not on IIS 7.0 or 7.5.

Any idea on how can it be done in IIS 7.0?

Thanks

Upvotes: 1

Views: 1839

Answers (2)

user1466890
user1466890

Reputation: 21

Solved:

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

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

new GenericPrincipal(new GenericIdentity("test"), null) will place "test" in the LOGON_USER variable

More information here: http://learn.iis.net/page.aspx/170/developing-a-module-using-net/

Upvotes: 1

Marco Miltenburg
Marco Miltenburg

Reputation: 6138

When setting server variables using the URL rewrite module of IIS you have to explicitly specify which server variables you allow to be set. This might be necessary for your own code as well. See: http://learn.iis.net/page.aspx/686/setting-http-request-headers-and-iis-server-variables/ (and search for 'Allowing server variables to be changed')

Upvotes: 0

Related Questions