Custom action runs with admin privilege and set session variables

311 views Asked by At

I want to run a custom action and seach registy and then set some session variables after that

heare is my custom action function

[CustomAction]
public static ActionResult RegistryDetails(Session session)
{
    try
    {
        CurrentSession = session;

        string registryPath = @"SOFTWARE\XXX\Printers";
        int printerIndex = 1;
        RegistryKey prnKey = Registry.LocalMachine;

        prnKey = prnKey.OpenSubKey(registryPath);

        List<string> subKeyList = new List<string>();
        subKeyList.AddRange(prnKey.GetSubKeyNames());

        while(subKeyList.Contains(printerIndex.ToString()))
        {
            printerIndex++;
        }

        string newRegistryPath = registryPath + "\\" + printerIndex.ToString();
        session["UtillRegKey"] = newRegistryPath;
        session["PrinterNo"] = printerIndex.ToString();
    }
    catch (Exception ex)
    {

        CurrentSession.Log(ex.Message);
        Record exceptionRec = new Record(0);
        exceptionRec[0] = "Errors -" + ex.StackTrace.ToString();
        return ActionResult.Failure;
    }

    return ActionResult.Success;
}

but to run this custom action, i need Admin privilages So I set Execute="deferred" & Impersonate="no" of my custom action definition. That makes session["PrinterNo"]&session["UtillRegKey"] inaccessible. because it should be Execute="immediate" to access session variables. but I cannot set as Execute="immediate", that will prevent run custom action run as admin.

Can anyone help me to overcome this.

1

There are 1 answers

0
Brian Sutherland On BEST ANSWER

You cannot modify session properties during the deferred (elevated) phase of the installation. You can read session properties during a deferred custom action and it requires a special property set by another custom action and using session.CustomActionData

It looks like you are only reading from the registry so there should be no problems doing this as an immediate action.