iishttpmoduleserver-variables

Replace backslash in REMOTE_USER Server Variable when authenticating with domain user iis httpModule


I am authenticating with a CRM running in IIS with domain accounts. I want to replace backslash in the REMOTE_USER server variable to return instead of Domain\User Domain_User or something of sorts because it throws an error when the backslash is return to the database. I tried writing a HTTPModule, but it returns "Operation is not supported on this platform" error on this line:

   oServerVars["REMOTE_USER"] = remote_user.Replace("\\", "_");

The code i am using is below:

private void Context_AuthenticateRequest(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;

        //Pulling out the Server Variables Collection
        NameValueCollection oServerVars = context.Request.ServerVariables;

        //Request REMOTE_USER Variable
        if (!string.IsNullOrEmpty(oServerVars["REMOTE_USER"]))
        {
            string remote_user = oServerVars["REMOTE_USER"];

            //Locate the server variables field in the collection   
            oServerVars = (NameValueCollection)context.Request.GetType().GetField("_serverVariables", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(context.Request);

            //Locate the readonly fields   
            PropertyInfo oReadable = oServerVars.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);

            //Setting the new value   
            oReadable.SetValue(oServerVars, false, null);
            oServerVars["REMOTE_USER"] = remote_user.Replace("\\", "_");
            oReadable.SetValue(oServerVars, true, null);
        }
    }

I have also tried to put the code in BeginRequest but no luck. Is there any way to achieve that?


Solution

  • Turns out it cannot be done. The only other way i could find is to create a new server variable, add it to the collection and call that one.