Search code examples
asp.netdelphicomwindows-authentication

How to get the current logged on user, including domain in Delphi 2009?


I need to get the current logged on username? I need this to work properly when I call the code from ASP.NET which is working in Windows Authentication mode. i.e. I do not want to get the ASPNET user in that circumstance, but the impersonated user. This is related to my earlier question. Everything I try returns ASPNET.


Solution

  • In your other question you wrote that you configured ASP.NET to use Windows authentication with impersonation:

     <system.web>
        ...
        <authentication mode="Windows"/>
        <identity impersonate="true"/>
        ...
     </system.web>
    

    Does the ASP.NET application show the correct credentials (user and domain)?

    Are you invoking the Delphi function using the correct Identity context, like

    WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
    try
    {
        ctx = winId.Impersonate();
        // call Delphi function, passing the identity context
    }
    catch
    {
    }
    finally
    {
        if (ctx != null)
            ctx.Undo();
    }
    

    Update:

    If the COM abject is called from the code behind for a web form page, you can try to set ASPCOMPAT property of the web form page to true.

    See:

    The "identity" tag makes sure that the thread executing the request (the MTA thread) will impersonate its security context to the user specified in the tag but our STA COM object eventually was created on the default STA thread which was not impersonate, causing it to get the security context of the process (which was IUSR_XXX – the least powerful user of all).